AVCaptureSession仅在iPhone 3G上返回空白图像 [英] AVCaptureSession returning blank image on iPhone 3G only

查看:102
本文介绍了AVCaptureSession仅在iPhone 3G上返回空白图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apple的AVCaptureSession示例代码,并且创建的UIImage完全空白。这只发生在iPhone 3G上,同时在控制台上显示一个独特的错误 -

I'm using Apple's example code for AVCaptureSession, and the UIImage that gets created is completely blank. This only happens on the iPhone 3G, along with a unique error that shows up on console that says -


错误:CGDataProviderCreateWithCopyOfData:vm_copy失败:状态2。

Error: CGDataProviderCreateWithCopyOfData: vm_copy failed: status 2.

我在线研究了错误,发现这个 StackOverflow回答,它消除了错误......但图像仍然是空白。

I've researched the error online and found this StackOverflow answer, and it gets rid of the error...but the image is still blank.

有没有其他人经历过这个并知道如何修复它?

Has anyone else experienced this and know how to fix it?

提前致谢。

我的代码 -

CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
CVPixelBufferLockBaseAddress(imageBuffer, 0); 

void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer); 
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
size_t width = CVPixelBufferGetWidth(imageBuffer); 
size_t height = CVPixelBufferGetHeight(imageBuffer); 

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); 
CGImageRef quartzImage = CGBitmapContextCreateImage(context); 
CVPixelBufferUnlockBaseAddress(imageBuffer,0);

CGContextRelease(context); 
CGColorSpaceRelease(colorSpace);

UIImage *image = [UIImage imageWithCGImage:quartzImage];

CGImageRelease(quartzImage);

return image;


推荐答案

API中的一些时髦因此我的工作使用的是:

something funky in the API so the work around that I'm using is:

//void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);     
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);     
size_t width = CVPixelBufferGetWidth(imageBuffer);     
size_t height = CVPixelBufferGetHeight(imageBuffer);     

// vm_copy fails on older model phones...    
//    
uint8_t *baseAddress = malloc( bytesPerRow * height );    
memcpy( baseAddress, CVPixelBufferGetBaseAddress(imageBuffer), bytesPerRow * height );

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();     

// NO ALPHA CHANNEL ON OLDER MODELS    
// CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);     
CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst);     
CGImageRef quartzImage = CGBitmapContextCreateImage(context);     

CVPixelBufferUnlockBaseAddress(imageBuffer,0);    
CGContextRelease(context);     
CGColorSpaceRelease(colorSpace);    
free( baseAddress );

UIImage *image = [UIImage imageWithCGImage:quartzImage];    
CGImageRelease(quartzImage);

return image;

这篇关于AVCaptureSession仅在iPhone 3G上返回空白图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆