CMSampleBuffer to UIImage in Swift

Gary Bartos
1 min readJun 6, 2021

If you have an AVCaptureVideoDataOutputSampleBufferDelegate, then your captureOutput function will pass a CMSampleBuffer.

Oh yay, another image data type.

If you want to convert that CMSampleBuffer to a UIImage for display, for saving, or whatnot, then you may find your answer here on StackOverflow:

Alternately, try the code below.

Note that my sample code includes the function imageWithCGImage(orientation:scale:). This is one of several cases in which creating a UIImage from other image type generates a UIImage without an underlying CGImage. Consequently this is only one of several conversion functions I use that returns an optional: UIImage? The function returns either a UIImage with a CGImage, or it returns nil if no CGImage can be created.

import UIKit
import AVFoundation

extension CMSampleBuffer {
/// https://stackoverflow.com/questions/15726761/make-an-uiimage-from-a-cmsamplebuffer
func image(orientation: UIImage.Orientation = .up, scale: CGFloat = 1.0) -> UIImage? {
if let buffer = CMSampleBufferGetImageBuffer(self) {
let ciImage = CIImage(cvPixelBuffer: buffer)

return UIImage(ciImage: ciImage, scale: scale, orientation: orientation)
}

return nil
}

func imageWithCGImage(orientation: UIImage.Orientation = .up, scale: CGFloat = 1.0) -> UIImage? {
if let buffer = CMSampleBufferGetImageBuffer(self) {
let ciImage = CIImage(cvPixelBuffer: buffer)

let context = CIContext(options: nil)

guard let cg = context.createCGImage(ciImage, from: ciImage.extent) else {
return nil
}

return UIImage(cgImage: cg, scale: scale, orientation: orientation)
}

return nil
}
}

--

--

Gary Bartos

Founder of Echobatix, developing assistive technology for the blind. echobatix@gmail.com