Note that there are some explanatory texts on larger screens.

plurals
  1. POLoading UIImage in background thread causes ImageIO error
    primarykey
    data
    text
    <p>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:</p> <pre><code>ImageIO: &lt;ERROR&gt; CGImageReadSessionGetCachedImageBlockData *** CGImageReadSessionGetCachedImageBlockData: bad readSession [0x685e590] </code></pre> <p>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 <code>initWithData:</code> method:</p> <pre><code>- (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]; } } </code></pre> <p>My <code>loadComplete:</code> method simply retains the image passed as an instance variable, which is later drawn in the view's <code>drawRect:</code> method. In <code>drawRect:</code>, I am using the UIImage <code>drawInRect:</code> method, which appears to be the perpetrator for the error message in the console. However, drawing the image's underlying CGImageRef using <code>CGContextDrawImage</code> still causes the same error. The full source code can be found in this project's Github repository, in the file <a href="https://github.com/unixpickle/ImageSlideshow/blob/master/ImageSlideshow/ANAsyncImageView.m" rel="nofollow">ANAsyncImageView.m</a>.</p> <p>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 <code>imageWithCGImage:</code> 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:</p> <pre><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); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload