6
如何將CVPixelBufferRef轉換爲NSImage或CGImageRef?我需要能夠在覈心動畫層中顯示像素緩衝區的內容。CVPixelBufferRef到NSImage
如何將CVPixelBufferRef轉換爲NSImage或CGImageRef?我需要能夠在覈心動畫層中顯示像素緩衝區的內容。CVPixelBufferRef到NSImage
要將緩衝區轉換爲圖像,需要先將緩衝區轉換爲CIImage
,然後才能將其轉換爲NSImage
。
let ciImage = CIImage(cvImageBuffer: pixelBuffer)
let context = CIContext(options: nil)
從這裏你可以去這兩個GCImage
和NSImage
:
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))