iOS UIImage的解码时机
2017-09-26 17:49
337 查看
在看博客 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。
这里有个问题, 当我们实例化一个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。
相关文章推荐
- ios 地理解码(地理位置转换经纬度)
- iOS UIImage:获取图片主色调
- ios开发-UIImageView适配图片大小
- iOS开发笔记--UIImageView的属性之animationImages详解
- iOS开发UI高级—37Quartz2D(自定义UIImageView控件)
- iOS UIImageView 大小调整
- iOS摄像头和相册-UIImagePickerControlle
- UIImage 图像-IOS开发
- 对于iOS的编码和解码
- iOS中的UIScorllView(滑动控件,时机控制)的基本使用
- ios中各数据类型转换 NSData转NSString,Byte,UIImage
- iOS UIImageView渲染
- iOS UITabBarItem 选中图的颜色,设置UIimage的渲染模式
- IOS 利用UIImageView实现加载动画
- iOS:通过URL构件UIImage
- IOS - UIImage
- [IOS 开发] 为UIImage 绘制 毛玻璃效果
- iOS 【读取图片数据 UIImageJPEGRepresentation和UIImagePNGRepresentation】
- iOS实现UIImageView透明区域点击事件穿透(OC&swift)
- iOS通过URL构建UIImage