I am attempting to convert a MTLTexture to a CGImage by adding an extension function to MTLTexture objects inside of my macOS playground.
The current function looks like this:
extension MTLTexture {
func toImage() -> CGImage? {
let context = CIContext()
let texture = self
let cImg = CIImage(mtlTexture: texture, options: nil)!
let cgImg = context.createCGImage(cImg, from: cImg.extent)!
return cgImg
}
}
However the line that is supposed to initialize cImg causes the concole to print Cannot render image (with an input Metal texture) using a metal context.
and return nil.
Why is this happening and how can I fix it?