I have a background thread that loads images and displays them in the main thread. I?noticed that the background thread has almost nothing to do, because the actual image decoding seems to be done in the main thread:
So far I’ve tried calling [UIImage imageNamed:]
, [UIImage imageWithData:]
and CGImageCreateWithJPEGDataProvider
in the background thread with no difference. Is there a way to force the decoding to be done on the background thread?
There’s already a similar question here, but it does not help. As I wrote there, I?tried the following trick:
@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
That works (forces the image to decode), but it also triggers an apparently expensive call to ImageIO_BGR_A_TO_RGB_A_8Bit
.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…