为什么这段代码解压缩 UIImage 比天真的方法好得多



在我的应用程序中,我需要加载大型JPEG图像并在滚动视图中显示它们。为了保持 UI 的响应,我决定在后台加载图像,然后在主线程上显示它们。为了在后台完全加载它们,我强制每个图像解压缩。我使用此代码解压缩图像(请注意,我的应用程序仅适用于iOS 7,因此我知道在后台线程上使用这些方法是可以的):

+ (UIImage *)decompressedImageFromImage:(UIImage *)image {
    UIGraphicsBeginImageContextWithOptions(image.size, YES, 0);
    [image drawAtPoint:CGPointZero];
    UIImage *decompressedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return decompressedImage;
}

但是,我仍然有很长的加载时间、UI 卡顿和很大的内存压力。我刚刚找到了另一种解决方案:

+ (UIImage *)decodedImageWithImage:(UIImage *)image {
    CGImageRef imageRef = image.CGImage;
    // System only supports RGB, set explicitly and prevent context error
    // if the downloaded image is not the supported format
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL,
                                                 CGImageGetWidth(imageRef),
                                                 CGImageGetHeight(imageRef),
                                                 8,
                                                 // width * 4 will be enough because are in ARGB format, don't read from the image
                                                 CGImageGetWidth(imageRef) * 4,
                                                 colorSpace,
                                                 // kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little
                                                 // makes system don't need to do extra conversion when displayed.
                                                 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
    CGColorSpaceRelease(colorSpace);
    if ( ! context) {
        return nil;
    }
    CGRect rect = (CGRect){CGPointZero, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)};
    CGContextDrawImage(context, rect, imageRef);
    CGImageRef decompressedImageRef = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    UIImage *decompressedImage = [[UIImage alloc] initWithCGImage:decompressedImageRef];
    CGImageRelease(decompressedImageRef);
    return decompressedImage;
}

此代码要好几个数量级。图像几乎立即加载,没有UI卡顿,内存使用量下降。

所以我的问题是双重的:

  1. 为什么第二种方法比第一种方法好得多?
  2. 如果第二种方法由于设备的唯一参数而更好,有没有办法确保它对所有iOS设备(现在和将来)同样有效?我不想假设本机位图格式会改变我,从而重新引入此问题。

我假设你在Retina设备上运行它。在 UIGraphicsBeginImageContextWithOptions 中,您要求默认比例,即主屏幕的比例,即 2。这意味着它生成的位图是原来的 4 倍。在第二个函数中,您以 1x 比例绘制。

尝试将 1 到 UIGraphicsBeginImageContextWithOptions 的等级传递,看看您的表现是否相似。

最新更新