title: How to convert a CVImageBufferRef to UIImage
body: <p>I am trying to capture video from a camera. i have gotten the <code>captureOutput:didOutputSampleBuffer:</code> callback to trigger and it gives me a sample buffer that i then convert to a <code>CVImageBufferRef</code>. i then attempt to convert that image to a <code>UIImage</code> that i can then view in my app.</p>
  
  <pre><code>- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
  {
      CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
      /*Lock the image buffer*/
      CVPixelBufferLockBaseAddress(imageBuffer,0); 
      /*Get information about the image*/
      uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer); 
      size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
      size_t width = CVPixelBufferGetWidth(imageBuffer); 
      size_t height = CVPixelBufferGetHeight(imageBuffer); 
  
      /*We unlock the  image buffer*/
      CVPixelBufferUnlockBaseAddress(imageBuffer,0);
  
      /*Create a CGImageRef from the CVImageBufferRef*/
      CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
      CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); 
      CGImageRef newImage = CGBitmapContextCreateImage(newContext); 
  
      /*We release some components*/
      CGContextRelease(newContext); 
       CGColorSpaceRelease(colorSpace);
  
       /*We display the result on the custom layer*/
      /*self.customLayer.contents = (id) newImage;*/
  
      /*We display the result on the image view (We need to change the orientation of the image so that the video is displayed correctly)*/
      UIImage *image= [UIImage imageWithCGImage:newImage scale:1.0 orientation:UIImageOrientationRight];
      self.capturedView.image = image;
  
      /*We relase the CGImageRef*/
      CGImageRelease(newImage);
  }
  </code></pre>
  
  <p>the code seems to work fine up until the call to <code>CGBitmapContextCreate</code>. it always returns a <code>NULL</code> pointer. so consequently none of the rest of the function works. no matter what i seem to pass it the function returns null. i have no idea why.</p>