我有一個後臺線程加載圖像並在主線程中顯示它們。我注意到,在後臺線程幾乎無關,因爲實際的圖像解碼似乎在主線程中完成:在後臺線程中解碼圖像?
到目前爲止,我已經試過在後臺調用[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