문제

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 -

Error: CGDataProviderCreateWithCopyOfData: vm_copy failed: status 2.

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?

Thanks in advance.

My Code -

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;
도움이 되었습니까?

해결책

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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top