为什么这个代码比天真的方法更好地解压缩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 ,看看你的表现是否相似。