title: Decode images in background thread?
body: <p>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:</p>
  
  <p><img src="https://i.stack.imgur.com/iEqgl.png" alt="alt text"></p>
  
  <p>So far I’ve tried calling <code>[UIImage imageNamed:]</code>, <code>[UIImage imageWithData:]</code> and <code>CGImageCreateWithJPEGDataProvider</code> in the background thread with no difference. Is there a way to force the decoding to be done on the background thread?</p>
  
  <p>There’s already <a href="https://stackoverflow.com/questions/1815476">a similar question</a> here, but it does not help. As I wrote there, I tried the following trick:</p>
  
  <pre><code>@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 &amp; 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
  </code></pre>
  
  <p>That works (forces the image to decode), but it also triggers an apparently expensive call to <code>ImageIO_BGR_A_TO_RGB_A_8Bit</code>.</p>