un'immagine vuota AVCaptureSession ritorno su iPhone 3G solo
-
08-10-2019 - |
Domanda
sto usando codice di esempio di Apple per AVCaptureSession, e l'UIImage che viene creato è completamente vuoto. Questo accade solo su iPhone 3G, insieme con un errore unico che si presenta sulla console che dice -
Errore: CGDataProviderCreateWithCopyOfData: vm_copy fallita:. Stato 2
Ho ricercato l'errore online e ho trovato questo StackOverflow risposta , e si libera dell'errore ... ma l'immagine è ancora vuota.
Qualcuno ha altro sperimentato questo e sa come risolvere il problema?
Grazie in anticipo.
Il mio Codice -
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;
Soluzione
qualcosa di strano nelle API in modo che il lavoro intorno che sto usando è:
//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;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow