Próbuję uchwycić wideo z kamery. Otrzymałem wywołanie zwrotne od captureOutput:didOutputSampleBuffer:
, które wyzwala i daje mi próbny bufor, który następnie konwertuję na CVImageBufferRef
. następnie próbuję przekonwertować ten obraz na UIImage
, który mogę następnie wyświetlić w mojej aplikacji.jak przekonwertować CVImageBufferRef na 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);
}
kod wydaje się działać poprawnie aż do połączenia z CGBitmapContextCreate
. zawsze zwraca wskaźnik NULL
. tak więc żadna z pozostałych funkcji nie działa. bez względu na to, co zdaję się przekazywać, funkcja zwraca wartość null. nie mam pojęcia dlaczego.
całkowicie prawidłowa odpowiedź, więc po prostu jako komentarz: kCVPixelFormatType_32RGBA nie jest dostępny jako format przechwytywania na iPhone 4, przynajmniej w iOS 4.2.1 . BGRA jest w pełni wdrożony. – Tommy