4

I'm using AVAssetReader/AVAssetWriter to convert my video on iOS. My question is: what's the most efficient way to show preview of current frame with real time conversion. I was thinking about converting CMSampleBufferRef to UIImage, then applying into UIImageView. It there any better way ?

user3128673
  • 663
  • 1
  • 5
  • 6

1 Answers1

4

I think AVSampleBufferDisplayLayer is what you're looking for.

It's a CALayer subclass that can display CMSampleBuffers and I imagine it's much faster than a trek through UIImage and UIImageView.

Here is a simple AVSampleBufferDisplayLayer-backed UIView implementation (in swift):

class SampleBufferView: UIView {
    override class func layerClass() -> AnyClass {
        return AVSampleBufferDisplayLayer.self
    }

    func enqueueSampleBuffer(sampleBuffer: CMSampleBuffer!) {
        let displayLayer = self.layer as! AVSampleBufferDisplayLayer
        displayLayer.enqueueSampleBuffer(sampleBuffer)
    }
}
Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159