title: Convert UIImage to CVImageBufferRef
body: <p>This code mostly works, but the resulting data seems to loose a color channel (is what I am thinking) as the resulting image data when displayed is tinted blue!</p>
  
  <p>Here is the code:</p>
  
  <pre><code>UIImage* myImage=[UIImage imageNamed:@"sample1.png"];
  CGImageRef imageRef=[myImage CGImage];
  CVImageBufferRef pixelBuffer = [self pixelBufferFromCGImage:imageRef];
  </code></pre>
  
  <p>The method pixelBufferFromCGIImage was grabbed from another post on stackoverflow here: <a href="https://stackoverflow.com/questions/3741323/how-do-i-export-uiimage-array-as-a-movie">How do I export UIImage array as a movie?</a> (although this application is unrelated to what I am trying to do) it is</p>
  
  <pre><code>+ (CVPixelBufferRef)pixelBufferFromCGImage:(CGImageRef)image
  {
      CGSize frameSize = CGSizeMake(CGImageGetWidth(image), CGImageGetHeight(image));
      NSDictionary *options = @{
                                (__bridge NSString *)kCVPixelBufferCGImageCompatibilityKey: @(NO),
                                (__bridge NSString *)kCVPixelBufferCGBitmapContextCompatibilityKey: @(NO)
                                };
      CVPixelBufferRef pixelBuffer;
      CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, frameSize.width,
                                            frameSize.height,  kCVPixelFormatType_32ARGB, (__bridge CFDictionaryRef) options,
                                            &amp;pixelBuffer);
      if (status != kCVReturnSuccess) {
          return NULL;
      }
  
      CVPixelBufferLockBaseAddress(pixelBuffer, 0);
      void *data = CVPixelBufferGetBaseAddress(pixelBuffer);
      CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
      CGContextRef context = CGBitmapContextCreate(data, frameSize.width, frameSize.height,
                                                   8, CVPixelBufferGetBytesPerRow(pixelBuffer), rgbColorSpace,
                                                   (CGBitmapInfo) kCGImageAlphaNoneSkipLast);
      CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),
                                             CGImageGetHeight(image)), image);
      CGColorSpaceRelease(rgbColorSpace);
      CGContextRelease(context);
      CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
  
      return pixelBuffer;
  }
  </code></pre>
  
  <p>I am thinking it has something to do with the relationship between kCVPixelFormatType_32ARGB and kCGImageAlphaNoneSkipLast though I have tried every combination and get either the same result or a application crash. Once again, this gets the UIImage data into CVImageBufferRef but when I display the image on screen, it appears to loose a color channel and shows up tinted blue. The image is a png.</p>