【发布时间】:2011-04-23 16:55:40
【问题描述】:
我有一个后台线程来加载图像并在主线程中显示它们。我注意到后台线程几乎无事可做,因为实际的图像解码似乎是在主线程中完成的:
到目前为止,我已经尝试在后台线程中调用[UIImage imageNamed:]、[UIImage imageWithData:] 和CGImageCreateWithJPEGDataProvider,没有任何区别。有没有办法强制在后台线程上进行解码?
这里已经有a similar question,但它没有帮助。正如我在那里写的那样,我尝试了以下技巧:
@implementation UIImage (Loading)
- (void) forceLoad
{
const CGImageRef cgImage = [self CGImage];
const int width = CGImageGetWidth(cgImage);
const int height = CGImageGetHeight(cgImage);
const CGColorSpaceRef colorspace = CGImageGetColorSpace(cgImage);
const CGContextRef context = CGBitmapContextCreate(
NULL, /* Where to store the data. NULL = don’t care */
width, height, /* width & height */
8, width * 4, /* bits per component, bytes per row */
colorspace, kCGImageAlphaNoneSkipFirst);
NSParameterAssert(context);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), cgImage);
CGContextRelease(context);
}
@end
这有效(强制图像解码),但它也会触发对ImageIO_BGR_A_TO_RGB_A_8Bit 的显然昂贵的调用。
【问题讨论】:
标签: iphone cocoa-touch uikit ios core-graphics