iOS硬解后获取的CVImageBufferRef转uiimage

之前网上搜索到很多的方法,但是会出问题,这里分享下!

-(UIImage *)cvsamplebufferrefToimage:(CVImageBufferRef)imageBuffer{

CVPixelBufferLockBaseAddress(imageBuffer, 0);

void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);

size_t width = CVPixelBufferGetWidth(imageBuffer);

size_t height = CVPixelBufferGetHeight(imageBuffer);

size_t bufferSize = CVPixelBufferGetDataSize(imageBuffer);

size_t bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, 0);

// Create a device-dependent gray color space

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

// Create a bitmap graphics context with the sample buffer data

CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGImageAlphaNone);

// Create a Quartz image from the pixel data in the bitmap graphics context

CGImageRef quartzImage = CGBitmapContextCreateImage(context);

// Free up the context and color space

CGContextRelease(context);

CGColorSpaceRelease(colorSpace);

// Create an image object from the Quartz image

UIImage *image = [UIImage imageWithCGImage:quartzImage];

return image;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容