问题
I am working on an iOS project that loads UIImages from URLs in a background thread and displays them in a horizontally paged scroll view. This behavior should mimic that of the native photo library application for iPhone. I have everything setup in a way that I believe should work, but upon drawing the UIImages in the main UI graphics context, I occasionally see this message print out in the console:
ImageIO: <ERROR> CGImageReadSessionGetCachedImageBlockData *** CGImageReadSessionGetCachedImageBlockData: bad readSession [0x685e590]
Despite this error, it appears that the app functions perfectly fine, and that the images draw as expected. Should I simply ignore this "ERROR," or should I change my method for loading images in the background thread? Currently, I load the image in a background thread using the UIImage initWithData:
method:
- (void)backgroundLoadThread:(NSURL *)url {
@autoreleasepool {
NSData * imageData = [NSData dataWithContentsOfURL:url];
UIImage * theImage = [[UIImage alloc] initWithData:imageData];
if ([[NSThread currentThread] isCancelled]) {
#if !__has_feature(objc_arc)
[theImage release];
#endif
return;
}
[self performSelectorOnMainThread:@selector(loadComplete:)
withObject:theImage waitUntilDone:NO];
}
}
My loadComplete:
method simply retains the image passed as an instance variable, which is later drawn in the view's drawRect:
method. In drawRect:
, I am using the UIImage drawInRect:
method, which appears to be the perpetrator for the error message in the console. However, drawing the image's underlying CGImageRef using CGContextDrawImage
still causes the same error. The full source code can be found in this project's Github repository, in the file ANAsyncImageView.m.
EDIT: I have now changed the background thread to load a CGImage from the file, and then callback to the main thread, passing the CGImageRef using toll-free bridging. Unfortunately, this still creates the same issue, proving to me that UIImage's imageWithCGImage:
must in some way keep a reference to the original CGImage. Check out the code in the file linked above (to the Github repository) for the full code:
NSData * imageData = [NSData dataWithContentsOfURL:url];
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)imageData);
CGImageRef loaded = CGImageCreateWithPNGDataProvider(provider, NULL, false, kCGRenderingIntentDefault);
CGDataProviderRelease(provider);
id imageObj = (id)loaded;
[self performSelectorOnMainThread:@selector(loadComplete:) withObject:imageObj waitUntilDone:NO];
CGImageRelease(loaded);
回答1:
UIKit is not a thread-safe class. Therefore, you should not call UIImage from the background.
Rather than create UIImages, render your images files into CGImageRefs. You can pass one of these back to the main thread and generate a UIImage from that (you will have still benefited by moving the heavy-lifting of image decompression to the background).
来源:https://stackoverflow.com/questions/8958178/loading-uiimage-in-background-thread-causes-imageio-error