ios – 为什么这段代码比天真的方法更好地解压缩UIImage?

在我的应用程序中,我需要加载大型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设备(现在和未来)同样有效?我不想假设我的原生位图格式发生了变化,重新引入了这个问题.

解决方法

我假设你在Retina设备上运行它.在UIGraphicsBeginImageContextWithOptions中,您询问了认比例,即主屏幕的比例,即2.这意味着它生成的位图大小为4倍.在第二个函数中,您以1x比例绘制.

尝试将1的比例传递给UIGraphicsBeginImageContextWithOptions,看看你的表现是否相似.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...