iOS CMSampleBuffer 转换 UIImage
CMSampleBuffer 转换 UIImage
第一种方法:
func WM_FUNC_sampleBufferToImage(_ sampleBuffer:CMSampleBuffer) -> UIImage {
let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!
let ciimage = CIImage(cvPixelBuffer: imageBuffer)
let context = CIContext(options: nil)
let cgImage = context.createCGImage(ciimage, from: ciimage.extent)!
let image = UIImage(cgImage: cgImage)
return image;
}
第二种方法:
import UIKit
import AVFoundation
extension 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
}
}
参考文章 convert-cmsamplebuffer-to-uiimage make-an-uiimage-from-a-cmsamplebuffer
|