CGBitmapContextCreateImage-vm_复制失败-iPhone SDK
我在iPhone应用程序中使用CGBitmapContextCreateImage时遇到问题CGBitmapContextCreateImage-vm_复制失败-iPhone SDK,iphone,objective-c,cgimage,Iphone,Objective C,Cgimage,我在iPhone应用程序中使用CGBitmapContextCreateImage时遇到问题 我使用AV基础框架用这种方法获取相机帧: - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection { CVImageBufferRef i
我使用AV基础框架用这种方法获取相机帧:
- (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.
但我不知道如何摆脱它。在功能上,它工作得很好。很明显,CGImage正在创建中,但我需要知道是什么导致了错误,这样才不会影响其他部分
非常感谢。任何帮助/建议都会很好!
布雷特免责声明:这纯粹是猜测。不再是了
vm\u copy()
是一个内核调用,用于将虚拟内存从一个位置复制到另一个位置()
您得到的返回值是KERN_PROTECTION_FAILURE,“源区域受到防读保护,或目标区域受到防写保护。”
因此,出于某种原因,CGDataProviderCreateWithCopyOfData调用此函数来复制一些内存,但失败了。也许它只是先尝试将vm_copy作为一种快速方法,然后再退回到一种较慢的方法(因为你说一切都是有效的)
如果您malloc
一块内存,将内存从baseAddress复制到您自己的内存中,并使用该内存创建映像,警告将消失。因此:
uint8_t *tmp = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
int bytes = ... // determine number of bytes from height * bytesperrow
uint8_t *baseAddress = malloc(bytes);
memcpy(baseAddress,tmp,bytes);
// unlock the memory, do other stuff, but don't forget:
free(baseAddress);
是的,我知道manpage是一个关于MacOSX的随机链接,但我想它也适用于iOS。我对同样的代码也有同样的问题。但问题只出现在iOS 4上的iPhone 3G设备上。它可以在iPhone4或iPhone3GS上正常工作。你能证实这一点吗?我可以证实我下面介绍的修复程序确实有效。我在装有iOS 4的3G上收到了vm_copy消息。
uint8_t *tmp = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
int bytes = ... // determine number of bytes from height * bytesperrow
uint8_t *baseAddress = malloc(bytes);
memcpy(baseAddress,tmp,bytes);
// unlock the memory, do other stuff, but don't forget:
free(baseAddress);