CMSampleBufferRef 与 UIImage 的转换

本文介绍如何将CMSampleBufferRef转换为UIImage的过程,包括获取CVImageBufferRef、CGContextRef、CGImageRef等步骤,并提供了详细的代码实现。

 转载自:http://iteches.com/archives/34496  (这里面还有其他 很多的信息 可看看)

CMSampleBufferRef 与 UIImage 的转换

在取得  CMSampleBufferRef 之后,还必须透过一连串的转换才能够得到 UIImage,CMSampleBufferRef –> CVImageBufferRef –> CGContextRef –> CGImageRef –> UIImage,你可以将以下程序代码任意实作于上述两个内部函数中来取得连续影像片段中的 UIImage。
//制作 CVImageBufferRef
CVImageBufferRef buffer;
buffer = CMSampleBufferGetImageBuffer(sampleBuffer);

CVPixelBufferLockBaseAddress(buffer, 0);

    //从 CVImageBufferRef 取得影像的细部信息
    uint8_t *base;
    size_t width, height, bytesPerRow;
    base = CVPixelBufferGetBaseAddress(buffer);
    width = CVPixelBufferGetWidth(buffer);
    height = CVPixelBufferGetHeight(buffer);
    bytesPerRow = CVPixelBufferGetBytesPerRow(buffer);

    //利用取得影像细部信息格式化 CGContextRef
    CGColorSpaceRef colorSpace;
    CGContextRef cgContext;
    colorSpace = CGColorSpaceCreateDeviceRGB();
    cgContext = CGBitmapContextCreate(base, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    CGColorSpaceRelease(colorSpace);

    //透过 CGImageRef 将 CGContextRef 转换成 UIImage
    CGImageRef cgImage;
    UIImage *image;
    cgImage = CGBitmapContextCreateImage(cgContext);
    image = [UIImage imageWithCGImage:cgImage];
    CGImageRelease(cgImage);
    CGContextRelease(cgContext);

CVPixelBufferUnlockBaseAddress(buffer, 0);

//成功转换成 UIImage
//[myImageView setImage:image];


最后,如果你希望改变撷取影像时的方向,则可以对内部函数中的 AVCaptureConnection 做 setVideoOrientation: 旋转影像,或 setVideoMirrored: 镜射影像。


#pragma mark - 截取照片 - (void)shutterCamera { AVCaptureConnection *videoConnection = [self.ImageOutPut connectionWithMediaType:AVMediaTypeVideo]; UIDeviceOrientation curDeviceOrientation = [[UIDevice currentDevice] orientation]; AVCaptureVideoOrientation avcaptureOrientation = [self avOrientationForDeviceOrientation:curDeviceOrientation]; [videoConnection setVideoOrientation:avcaptureOrientation]; [videoConnection setVideoScaleAndCropFactor:self.effectiveScale]; if (!videoConnection || !self.session.isRunning) { ATLog(@"take photo failed!"); return; } MJWeakSelf; [self.ImageOutPut captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ if (imageDataSampleBuffer) { NSData * imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; weakSelf.image = [UIImage imageWithData:imageData]; if (weakSelf.image) { weakSelf.image = [UIImage updateImageSize:weakSelf.image]; } weakSelf.image = [[TZImageManager manager] fixOrientation:weakSelf.image];//[UIImage fixOrientation:weakSelf.image]; PHAuthorizationStatus authorStatus = [PHPhotoLibrary authorizationStatus]; if (authorStatus == PHAuthorizationStatusAuthorized) { [[TZImageManager manager] savePhotoWithImage:weakSelf.image completion:^(PHAsset *asset, NSError *error) { [weakSelf addPHAsset:asset]; }]; }else{ [weakSelf gotoEditImageController:weakSelf.image asset:nil]; } } }); }]; }优化下此段代码
07-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值