Tôi đang cố gắng quay video từ máy ảnh. tôi đã nhận được gọi lại captureOutput:didOutputSampleBuffer:
để kích hoạt và nó mang lại cho tôi một bộ đệm mẫu mà sau đó tôi chuyển đổi thành CVImageBufferRef
. sau đó tôi cố gắng chuyển đổi hình ảnh đó thành một số UIImage
mà tôi có thể xem trong ứng dụng của mình.cách chuyển đổi CVImageBufferRef sang 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);
}
mã dường như hoạt động tốt cho đến khi cuộc gọi đến CGBitmapContextCreate
. nó luôn trả về con trỏ NULL
. do đó không có phần còn lại của hàm hoạt động. không có vấn đề gì tôi dường như vượt qua nó hàm trả về null. Tôi không biết tại sao.
hoàn toàn câu trả lời đúng, vì vậy cũng giống như một lời nhận xét: kCVPixelFormatType_32RGBA không phải là có sẵn như là một định dạng chụp trên iPhone 4 ít nhất trong iOS 4.2.1 . BGRA được thực hiện đầy đủ. – Tommy