我有一个后台线程加载图像并在主线程中显示它们。我注意到,在后台线程几乎无关,因为实际的图像解码似乎在主线程中完成:在后台线程中解码图像?
到目前为止,我已经试过在后台调用[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
。
谢谢,效果很好。 – Morrowless 2014-06-11 07:09:39