title: UIImage created from CMSampleBufferRef not displayed in UIImageView?
body: <p>I'm trying to display a UIImage in real-time coming from the camera, and it seems that my UIImageView is not displaying the image properly. This is the method which a <code>AVCaptureVideoDataOutputSampleBufferDelegate</code> has to implement</p>
  
  <pre><code>- (void)captureOutput:(AVCaptureOutput *)captureOutput 
  didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
         fromConnection:(AVCaptureConnection *)connection
  { 
      // Create a UIImage from the sample buffer data
      UIImage *theImage = [self imageFromSampleBuffer:sampleBuffer];
  //    NSLog(@"Got an image! %f %f", theImage.size.width, theImage.size.height);
  //    NSLog(@"The image view is %@", imageView);
  //    UIImage *theImage = [[UIImage alloc] initWithData:[NSData 
  //        dataWithContentsOfURL:[NSURL 
  //        URLWithString:@"http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"]]];
      [self.session stopRunning];
      [imageView setImage: theImage];
  }
  </code></pre>
  
  <p>To get the easy problems out of the way: </p>
  
  <ul>
  <li>Using UIImagePickerController is not an option (eventually we will actually do things with the image)</li>
  <li>I know the handler is being called (the NSLog calls are made, and I see the output)</li>
  <li>I know I have the IBOutlet declarations set up correctly. If I use the commented code above to load an arbitrary image from the web instead of simply sending <code>setImage:theImage</code> to the imageView, the image is loaded correctly (and the second call to NSLog reports a non-nil object).</li>
  <li>At least to a basic extent, the image I get from <code>imageFromSampleBuffer:</code> is fine, since NSLog reports the size to be 360x480, which is the size I expected.</li>
  </ul>
  
  <p>The code I'm using is the recently-posted <code>AVFoundation</code> snippet from Apple available <a href="http://developer.apple.com/iphone/library/qa/qa2010/qa1702.html" rel="nofollow noreferrer">here</a>.</p>
  
  <p>In particular, that is the code I use which sets up the <code>AVCaptureSession</code> object and friends (of which I understand very little), and creates the UIImage object from the Core Video buffers (that's the <code>imageFromSampleBuffer</code> method).</p>
  
  <p>Finally, I can get the application to crash if I try to send <code>drawInRect:</code> to a plain UIView subclass with the <code>UIImage</code> returned by <code>imageFromSamplerBuffer</code>, while it doesn't crash if I use an <code>UIImage</code> from a URL as above. Here is the stack trace from the debugger inside the crash (I get a EXC_BAD_ACCESS signal):</p>
  
  <pre><code>#0  0x34a977ee in decode_swap ()
  #1  0x34a8f80e in decode_data ()
  #2  0x34a8f674 in img_decode_read ()
  #3  0x34a8a76e in img_interpolate_read ()
  #4  0x34a63b46 in img_data_lock ()
  #5  0x34a62302 in CGSImageDataLock ()
  #6  0x351ab812 in ripc_AcquireImage ()
  #7  0x351a8f28 in ripc_DrawImage ()
  #8  0x34a620f6 in CGContextDelegateDrawImage ()
  #9  0x34a61fb4 in CGContextDrawImage ()
  #10 0x321fd0d0 in -[UIImage drawInRect:blendMode:alpha:] ()
  #11 0x321fcc38 in -[UIImage drawInRect:] ()
  </code></pre>
  
  <p>EDIT: Here's some more information about the UIImage being returned by that bit of code.</p>
  
  <p>Using the method described <a href="https://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more">here</a>, I can get to the pixels and print them, and they look ok at first glance (every value in the alpha channel is 255, for example). However, there's something slightly off with the buffer sizes. The image I get from Flickr from that URL is 375x500, and its <code>[pixelData length]</code> gives me <code>750000 = 375*500*4</code>, which is the expected value. However, the pixel data of image returned from <code>imageFromSampleBuffer:</code> has size <code>691208 = 360*480*4 + 8</code>, so there's 8 extra bytes in the pixel data. <code>CVPixelBufferGetDataSize</code> itself returns this off-by-8 value. I thought for a moment that it could be due to allocating buffers at aligned positions in memory, but 691200 is a multiple of 256, so that doesn't explain it either. This size discrepancy is the only difference I can tell between the two UIImages, and it could be causing the trouble. Still, there's no reason allocating <em>extra</em> memory for the buffer should cause a EXC_BAD_ACCESS violation.</p>
  
  <p>Thanks a lot for any help, and let me know if you need more information.</p>