您可以从基地址数据创建数据提供者而无需复制,然后从该数据提供者创建 UIImage。为避免在引用此图像时重复使用缓冲区,您需要保留样本缓冲区并锁定基地址。当您忘记此图像对象时,它们应该被自动解锁和释放:
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
// Retain sample buffer and lock base address
CFRetain(sampleBuffer);
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer, 0);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
void *baseAddress = (void *)CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0);
UIImage *image = imageFromData(baseAddress, width, height, bytesPerRow, sampleBuffer);
// Now you can store this UIImage as long as you want
}
我imageFromData
从这个项目中得到了https://github.com/k06a/UIImage-DecompressAndMap/blob/master/UIImage%2BDecompressAndMap.m并采用了一点:
UIImage *imageFromData(void *data, size_t width, size_t height, size_t bytesPerRow, CMSampleBufferRef sampleBuffer)
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGDataProviderRef provider = CGDataProviderCreateWithData((void *)sampleBuffer, data, bytesPerRow * height, munmap_wrapper);
CGImageRef inflatedImage = CGImageCreate(width, height, 8, 4*8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst, provider, NULL, NO, kCGRenderingIntentDefault);
CGColorSpaceRelease(colorSpace);
CGDataProviderRelease(provider);
UIImage *img = [UIImage imageWithCGImage:inflatedImage scale:scale orientation:UIImageOrientationUp];
CGImageRelease(inflatedImage);
return img;
}
您还需要提供unlock_function
:
void unlock_function(void *info, const void *data, size_t size)
{
// Unlock base address release sample buffer
CMSampleBufferRef sampleBuffer = (CMSampleBufferRef)info;
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
CFRelease(sampleBuffer);
}