It crashes because kCVPixelFormatType_24RGB
isn't supported for iPhone. The only pixel formats that are supported in modern iPhones are:
- kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange
- kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
- kCVPixelFormatType_32BGRA
You can convert any of these to RGB, although the BGRA buffer is simpler to convert. There are various ways to do it (search here and on Google for examples), but here's a pretty straightforward approach:
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
@autoreleasepool {
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
uint8_t *sourceBuffer = (uint8_t*)CVPixelBufferGetBaseAddress(imageBuffer);
CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
int bufferSize = bytesPerRow * height;
uint8_t *bgraData = malloc(bufferSize);
memcpy(bgraData, sourceBuffer, bufferSize);
uint8_t *rgbData = malloc(width * height * 3);
int rgbCount = 0;
for (int i = 0; i < height; i++) {
for (int ii = 0; ii < width; ii+=4) {
int current = (i * height)+ii;
rgbData[rgbCount] = bgraData[current + 2];
rgbData[rgbCount + 1] = bgraData[current + 1];
rgbData[rgbCount + 2] = bgraData[current];
rgbCount+=3;
}
}
//
// Process rgbData
//
free (rgbData);
}
}
Incidentally—it's 8bpp (not 24bpp); three eight-bit planes making up the 24-bit image, or four planes making the 32-bit image. It's also worth pointing out that it's probably easier and quicker, in most cases, just to work with the 32-bit data and ignore the alpha channel, rather than converting to 24-bit.