使用太多内存减少图像尺寸...是否有办法使用更少的内存?

时间:2013-11-08 11:45:37

标签: ios iphone ipad cocoa-touch

我有一个应用程序使用相机拍照。拍摄照片后,我会减小来自相机的图像尺寸。

运行缩小图像大小的方法,使内存使用率达到21 MB到61 MB,有时接近69 MB!

我已将@autoreleasepool添加到此过程中涉及的每个方法。事情有所改善,但没有我想象的那么多。减少图像时我不希望内存使用量跳跃3次,特别是因为生成的新图像较小。

这些是我尝试过的方法:

- (UIImage*)reduceImage:(UIImage *)image toSize:(CGSize)size {

    @autoreleasepool {
        UIGraphicsBeginImageContext(size);

        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(context, 0.0, size.height);
        CGContextScaleCTM(context, 1.0, -1.0);

        CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), image.CGImage);

        UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        return scaledImage;

    }
}

以及

- (UIImage *)reduceImage:(UIImage *)image toSize:(CGSize)size {

    @autoreleasepool {
        UIGraphicsBeginImageContext(size);

        [image drawInRect:rect];
        UIImage * result =  UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        return result;
    }
}

这两者之间没有任何区别。

注意:原始图像为3264x2448像素x 4字节/像素= 32MB,最终图像为1136x640,即2.9MB ...两个数字相加,得到35MB,而不是70!

有没有办法减少图像的大小而不会使内存使用达到峰值?感谢。

顺便说一句,出于好奇:有没有办法在不使用Quartz的情况下缩小图像尺寸?

1 个答案:

答案 0 :(得分:0)

答案是here

使用CoreGraphics并减少30%~40%的内存。

    #import <ImageIO/ImageIO.h>
   -(UIImage*) resizedImageToRect:(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;
}

作为UIImage的类别添加。