在我的应用程序中,我需要加载大型JPEG图像并在滚动视图中显示它们.为了保持UI响应,我决定在后台加载图像,然后在主线程上显示它们.为了在后台完全加载它们,我强制每个图像被解压缩.我正在使用此代码解压缩图像(请注意,我的应用程序仅限iOS 7,因此我了解在后台线程上使用这些方法是可以的):
- + (UIImage *)decompressedImageFromImage:(UIImage *)image {
- UIGraphicsBeginImageContextWithOptions(image.size,YES,0);
- [image drawAtPoint:CGPointZero];
- UIImage *decompressedImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return decompressedImage;
- }
但是,我仍然有很长的加载时间,UI口吃和很多内存压力.我刚发现another solution:
- + (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,CGImageGetHeight(imageRef)};
- CGContextDrawImage(context,rect,imageRef);
- CGImageRef decompressedImageRef = CGBitmapContextCreateImage(context);
- CGContextRelease(context);
- UIImage *decompressedImage = [[UIImage alloc] initWithCGImage:decompressedImageRef];
- CGImageRelease(decompressedImageRef);
- return decompressedImage;
- }
此代码的数量级更好.图像几乎立即加载,没有UI断断续续,内存使用率已经下降.
所以我的问题是双重的:
>为什么第二种方法比第一种方法好得多?
>如果第二种方法由于设备的独特参数而更好,是否有办法确保它对所有iOS设备(现在和未来)同样有效?我不想假设我的原生位图格式发生了变化,重新引入了这个问题.