我在我的 iPhone 应用程序中使用 CGBitmapContextCreateImage 时遇到问题。
我正在使用 AV Foundation Framework 使用这种方法来抓取相机帧:
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef newImage = CGBitmapContextCreateImage(newContext);
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
CGContextRelease(newContext);
CGColorSpaceRelease(colorSpace);
UIImage *image= [UIImage imageWithCGImage:newImage scale:1.0 orientation:UIImageOrientationRight];
self.imageView.image = image;
CGImageRelease(newImage);
}
但是,我在调试控制台运行时看到一个错误:
<Error>: CGDataProviderCreateWithCopyOfData: vm_copy failed: status 2.
有没有人见过这个?通过注释掉行,我将问题范围缩小到:
CGImageRef newImage = CGBitmapContextCreateImage(newContext);
但我不知道如何摆脱它。从功能上讲,它工作得很好。很明显,正在创建 CGImage,但我需要知道是什么导致了错误,所以它不会影响其他部分。
非常感谢。任何帮助/建议都会很棒!布雷特