如何将CVPixelBufferRef转换为NSImage或CGImageRef?我需要能够在核心动画层中显示像素缓冲区的内容。

最佳答案

要将缓冲区转换为图像,需要首先将缓冲区转换为CIImage,然后可以将其转换为NSImage

let ciImage = CIImage(cvImageBuffer: pixelBuffer)

let context = CIContext(options: nil)

从这里您可以转到GCImageNSImage:
let width = CVPixelBufferGetWidth(pixelBuffer)
let height = CVPixelBufferGetHeight(pixelBuffer)

let cgImage = context.createCGImage(ciImage, from: CGRect(x: 0, y: 0, width: width, height: height))

let nsImage = NSImage(cgImage: cgImage, size: CGSize(width: width, height: height))

关于macos - CVPixelBuffer引用NSImage,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10318260/

10-11 17:50