how to convert a CVImageBufferRef to UIImage

后端 未结 4 1235
别跟我提以往
别跟我提以往 2020-11-28 21:52

I am trying to capture video from a camera. i have gotten the captureOutput:didOutputSampleBuffer: callback to trigger and it gives me a sample buffer that i th

4条回答
  •  情歌与酒
    2020-11-28 22:15

    If you simply need to convert a CVImageBufferRef to UIImage, it seems to be much more difficult than it should be. Essentially you need to convert to CIImage, then CGImage, THEN UIImage. I wish I could tell you why. Who knows.

    -(void) screenshotOfVideoStream:(CVImageBufferRef)imageBuffer
    {
        CIImage *ciImage = [CIImage imageWithCVPixelBuffer:imageBuffer];
        CIContext *temporaryContext = [CIContext contextWithOptions:nil];
        CGImageRef videoImage = [temporaryContext
                                 createCGImage:ciImage
                                 fromRect:CGRectMake(0, 0,
                                 CVPixelBufferGetWidth(imageBuffer),
                                 CVPixelBufferGetHeight(imageBuffer))];
    
        UIImage *image = [[UIImage alloc] initWithCGImage:videoImage];
        [self doSomethingWithOurUIImage:image];
        CGImageRelease(videoImage);
    }
    

    This particular method worked for me when I was converting H.264 video using the VTDecompressionSession callback to get the CVImageBufferRef (but it should work for any CVImageBufferRef). I was using iOS 8.1, XCode 6.2.

提交回复
热议问题