Hil*_*ell 9 performance objective-c uiimage ios
在我的应用程序中,我需要加载大型JPEG图像并在滚动视图中显示它们.为了保持UI响应,我决定在后台加载图像,然后在主线程上显示它们.为了在后台完全加载它们,我强制每个图像都要解压缩.我正在使用此代码解压缩图像(请注意,我的应用程序仅限iOS 7,因此我了解在后台线程上使用这些方法是可以的):
+ (UIImage *)decompressedImageFromImage:(UIImage *)image {
UIGraphicsBeginImageContextWithOptions(image.size, YES, 0);
[image drawAtPoint:CGPointZero];
UIImage *decompressedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return decompressedImage;
}
Run Code Online (Sandbox Code Playgroud)
但是,我仍然有很长的加载时间,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;
}
Run Code Online (Sandbox Code Playgroud)
此代码的数量级更好.图像几乎立即加载,没有UI断断续续,内存使用率已经下降.
所以我的问题是双重的:
我假设你在Retina设备上运行它.在UIGraphicsBeginImageContextWithOptions
,您要求默认比例,即主屏幕的比例,即2.这意味着它生成的位图大小为4倍.在第二个函数中,您以1x比例绘制.
尝试传递1的比例,UIGraphicsBeginImageContextWithOptions
看看你的表现是否相似.
归档时间: |
|
查看次数: |
2300 次 |
最近记录: |