AVCaptureSession returns blank image only on iPhone 3G

I am using Apple sample code for AVCaptureSession, and the UIImage created is completely empty. This only happens on the iPhone 3G, as well as a unique error that appears on the console that says -

Error: CGDataProviderCreateWithCopyOfData: vm_copy failed: status 2.

I researched the error on the Internet and found this https://stackoverflow.com/a/166189/ and it gets rid of the error ... but the image is still empty.

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

Thanks in advance.

My code is

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;
+3
source share
1 answer

- API, , , :

//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;
+6

Source: https://habr.com/ru/post/1611122/


All Articles