我正在尝试将 CVPixelBuffer 的大小调整为 128x128。我正在使用 750x750 的。我目前正在使用 CVPixelBuffer 创建一个新的 CGImage,我调整它的大小然后转换回 CVPixelBuffer。这是我的代码:
func getImageFromSampleBuffer (buffer:CMSampleBuffer) -> UIImage? {
if let pixelBuffer = CMSampleBufferGetImageBuffer(buffer) {
let ciImage = CIImage(cvPixelBuffer: pixelBuffer)
let context = CIContext()
let imageRect = CGRect(x: 0, y: 0, width: 128, height: 128)
if let image = context.createCGImage(ciImage, from: imageRect) {
let t = CIImage(cgImage: image)
let new = t.applying(transformation)
context.render(new, to: pixelBuffer)
return UIImage(cgImage: image, scale: UIScreen.main.scale, orientation: .right)
}
}
return nil
}
我也尝试过缩放 CIImage 然后转换它:
let t = CIImage(cgImage: image)
let transformation = CGAffineTransform(scaleX: 1, y: 2)
let new = t.applying(transformation)
context.render(new, to: pixelBuffer)
但这也没有用。
任何帮助表示赞赏。谢谢!