Any code/library to scale down an UIImage?

后端 未结 4 457
北荒
北荒 2021-01-01 04:43

Is there any code or library out there that can help me scale down an image? If you take a picture with the iPhone, it is something like 2000x1000 pixels which is not very n

相关标签:
4条回答
  • 2021-01-01 05:03

    This is what I am using. Works well. I'll definitely be watching this question to see if anyone has anything better/faster. I just added the below to a category on UIimage.

    + (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize {
      UIGraphicsBeginImageContext( newSize );
      [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
      UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
    
      return newImage;
    }
    
    0 讨论(0)
  • 2021-01-01 05:05

    See http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/ - this has a set of code you can download as well as some descriptions.

    If speed is a worry, you can experiment with using CGContextSetInterpolationQuality to set a lower interpolation quality than the default.

    0 讨论(0)
  • 2021-01-01 05:05

    Please note, this is NOT my code. I did a little digging and found it here. I figured you'd have to drop into the CoreGraphics layer, but wasn't quite sure of the specifics. This should work. Just be careful about managing your memory.

    //  ==============================================================
    //  resizedImage
    //  ==============================================================
    // Return a scaled down copy of the image.  
    
    UIImage* resizedImage(UIImage *inImage, CGRect thumbRect)
    {
        CGImageRef          imageRef = [inImage CGImage];
        CGImageAlphaInfo    alphaInfo = CGImageGetAlphaInfo(imageRef);
    
        // There's a wierdness with kCGImageAlphaNone and CGBitmapContextCreate
        // see Supported Pixel Formats in the Quartz 2D Programming Guide
        // Creating a Bitmap Graphics Context section
        // only RGB 8 bit images with alpha of kCGImageAlphaNoneSkipFirst, kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst,
        // and kCGImageAlphaPremultipliedLast, with a few other oddball image kinds are supported
        // The images on input here are likely to be png or jpeg files
        if (alphaInfo == kCGImageAlphaNone)
            alphaInfo = kCGImageAlphaNoneSkipLast;
    
        // Build a bitmap context that's the size of the thumbRect
        CGContextRef bitmap = CGBitmapContextCreate(
                    NULL,
                    thumbRect.size.width,       // width
                    thumbRect.size.height,      // height
                    CGImageGetBitsPerComponent(imageRef),   // really needs to always be 8
                    4 * thumbRect.size.width,   // rowbytes
                    CGImageGetColorSpace(imageRef),
                    alphaInfo
            );
    
        // Draw into the context, this scales the image
        CGContextDrawImage(bitmap, thumbRect, imageRef);
    
        // Get an image from the context and a UIImage
        CGImageRef  ref = CGBitmapContextCreateImage(bitmap);
        UIImage*    result = [UIImage imageWithCGImage:ref];
    
        CGContextRelease(bitmap);   // ok if NULL
        CGImageRelease(ref);
    
        return result;
    }
    
    0 讨论(0)
  • 2021-01-01 05:15

    Please see the solution I posted to this question. The question involves rotating an image 90 degrees instead of scaling it, but the premise is the same (it's just the matrix transformation that is different).

    0 讨论(0)
提交回复
热议问题