CVPixelBufferRef for NSImage

How to convert CVPixelBufferRef to NSImage or CGImageRef? I need to be able to display the contents of a pixel buffer in a Core Animation Layer.

+6
source share
1 answer

To convert a buffer to an image, you first need to convert the buffer to CIImage , which you can then convert to NSImage .

 let ciImage = CIImage(cvImageBuffer: pixelBuffer) let context = CIContext(options: nil) 

From here you can go to GCImage and 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)) 
0
source

Source: https://habr.com/ru/post/914138/


All Articles