本文介绍了CGBitmapContextCreateImage - vm_copy失败 - iPhone SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在iPhone应用程序中使用CGBitmapContextCreateImage时出现问题。I am having an issue using CGBitmapContextCreateImage in my iPhone app.我正在使用AV Foundation Framework使用此方法抓取相机帧:I am using AV Foundation Framework to grab camera frames using this method:- (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);} 但是,我在调试控制台中看到一个错误它的运行:However, I am seeing an error in the debug console as its runs:<Error>: CGDataProviderCreateWithCopyOfData: vm_copy failed: status 2.有没有人见过这个?通过注释掉我已将问题界限缩小到:Has anyone ever seen this? By commenting out lines I have narrowed the problem line out to:CGImageRef newImage = CGBitmapContextCreateImage(newContext);但我不知道如何摆脱它。从功能上讲,它很棒。很明显,CGImage正在创建,但我需要知道是什么导致错误,所以它不会影响其他部分。but I am not sure how to get rid of it. Functionally, it works great. So clearly, the CGImage is being created, but I need to know what is causing the error so it doesn't affect other parts.非常感谢。任何帮助/建议都会很棒! Brett Many thanks. Any help/advice would be great!Brett推荐答案 Not anymore. vm_copy()是将虚拟内存从一个地方复制到另一个地方的内核调用(联机帮助页)。vm_copy() is a kernel call to copy virtual memory from one place to another (manpage).您获得的返回值是KERN_PROTECTION_FAILURE,源区域受到保护以防止读取,或者目标区域受到保护以免写入。The return value you get is KERN_PROTECTION_FAILURE, "The source region is protected against reading, or the destination region is protected against writing."所以对于某些人来说原因CGDataProviderCreateWithCopyOfData调用它来复制一些内存,并失败。 也许它只是首先尝试将vm_copy作为快速方法,然后再回到较慢的方法(因为你说一切正常)。So for some reason CGDataProviderCreateWithCopyOfData calls this to copy some memory, and fails. maybe it is just trying vm_copy as a fast method first, and then falls back to a slower method (since you say everything works).如果您 malloc 一块内存,将内存从baseAddress记忆到您自己的内存,并使用它来创建图像,警告就会消失。所以:If you malloc a chunk of memory, memcpy the memory from baseAddress to your own memory, and use that to create the image, the warning vanishes. So:uint8_t *tmp = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);int bytes = ... // determine number of bytes from height * bytesperrowuint8_t *baseAddress = malloc(bytes);memcpy(baseAddress,tmp,bytes);// unlock the memory, do other stuff, but don't forget:free(baseAddress); 这篇关于CGBitmapContextCreateImage - vm_copy失败 - iPhone SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-21 13:01