1

我有一个使用相机拍照的应用程序。拍完照片后,我会缩小来自相机的图像的大小。

运行减小图像大小的方法,使内存使用峰值从 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 的情况下减小图像尺寸?

4

1 回答 1

0

答案在这里

使用 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。

于 2013-11-08T18:48:04.697 回答