Ich versuche, Video von einer Kamera zu erfassen. Ich habe die captureOutput:didOutputSampleBuffer:
Callback zu triggern und es gibt mir einen Beispielpuffer, den ich dann in eine CVImageBufferRef
konvertieren. Ich versuche dann, das Bild in eine UIImage
konvertieren, die ich dann in meiner App anzeigen kann.Wie konvertiert man einen CVImageBufferRef zu UIImage
- (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);
}
der Code scheint gut, bis der Anruf CGBitmapContextCreate
aufzuarbeiten. es gibt immer einen Zeiger NULL
zurück. also funktioniert nichts vom Rest der Funktion. egal, was ich zu übergeben scheint, die Funktion gibt null zurück. ich habe keine Idee warum.
Ganz die richtige Antwort, so einfach als Kommentar: kCVPixelFormatType_32RGBA ist als Capture-Format auf dem iPhone 4 zumindest in iOS 4.2.1 nicht verfügbar . BGRA ist vollständig implementiert. – Tommy