iOS UIImage的解码时机

本文探讨了在iOS开发中,UIImage对象的解码时机及其对性能的影响。默认情况下,CPU并不会立即解码jpg/png图像,而是在GPU显示时才进行解码,这可能导致大量图像处理时的主线程卡顿。第三方库如SDWebImage通过缓存未解码图片并在显示前解码来优化这一过程,而Kingfisher尚未实现这一特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     在看博客 UITableView优化技巧 时想到列表的优化主要还是对图片的优化处理。 博文中介绍了按需加载、快速滑动时不加载、异步刷新等等技巧。

      这里有个问题, 当我们实例化一个UIImage对象并为UIImageView的image赋值时, 到底有没有将jpg/png转换为bitmap格式呢?  答案是否定的, 默认情况下CPU不会对图像进行解码; 当CPU将图像数据传送给GPU时, GPU会判断数据格式并转换为Bitmap格式。

       PS: GPU只能显示Bitmap格式的图像,而且GPU的解码操作运行在主线程,绕不过去的;所以UI要显示大量的png/jpg/gif时, 可能会卡顿!

       为什么我们在日常开发中没发现这个问题呢? 其实是三方图像库帮我们做过优化了。  下面看看主流的SDWebImage是怎么做的, 缓存中可以存储未解码图片, 当需要显示到界面时一定会执行解码函数decodedImageWithImage。

+ (nullable UIImage *)decodedImageWithImage:(nullable UIImage *)image {
    if (![UIImage shouldDecodeImage:image]) {
        return image;
    }
    
    // autorelease the bitmap context and all vars to help system to free memory when there are memory warning.
    // on iOS7, do not forget to call [[SDImageCache sharedImageCache] clearMemory];
    @autoreleasepool{
        
        CGImageRef imageRef = image.CGImage;
        CGColorSpaceRef colorspaceRef = [UIImage colorSpaceForImageRef:imageRef];
        
        size_t width = CGImageGetWidth(imageRef);
        size_t height = CGImageGetHeight(imageRef);
        size_t bytesPerRow = kBytesPerPixel * width;

        // kCGImageAlphaNone is not supported in CGBitmapContextCreate.
        // Since the original image here has no alpha info, use kCGImageAlphaNoneSkipLast
        // to create bitmap graphics contexts without alpha info.
        CGContextRef context = CGBitmapContextCreate(NULL,
                                                     width,
                                                     height,
                                                     kBitsPerComponent,
                                                     bytesPerRow,
                                                     colorspaceRef,
                                                     kCGBitmapByteOrderDefault|kCGImageAlphaNoneSkipLast);
        if (context == NULL) {
            return image;
        }
        
        // Draw the image into the context and retrieve the new bitmap image without alpha
        CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
        CGImageRef imageRefWithoutAlpha = CGBitmapContextCreateImage(context);
        UIImage *imageWithoutAlpha = [UIImage imageWithCGImage:imageRefWithoutAlpha
                                                         scale:image.scale
                                                   orientation:image.imageOrientation];
        
        CGContextRelease(context);
        CGImageRelease(imageRefWithoutAlpha);
        
        return imageWithoutAlpha;
    }
}

      即在子线程中将图片格式转换为Bitmap, 然后赋值给UIImageView的image参数显示到界面。


      对比一下喵神写的Kingfisher, 并没有这个转码操作,相信以后的版本可能会加上这个Feature。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值