I have the following piece of code:
This code crashes on line 14 with EXC_BAD_ACCESS code 1 without additional calls. However, when I set options to nil in CIImage call on line 5, it renders fine.
FWIW, there's no change if I remove lines 9-13 and call createCGImage straight from image.
It also crashes, if I instantiate the context without the device:
return CIContext().createCGImage(...)
What am I missing?
Code Block swift extension MTLTexture { public var cgImage: CGImage? { let ctx = CIContext(mtlDevice: self.device) let options: [CIImageOption: Any] = [CIImageOption.colorSpace: CGColorSpace.linearSRGB] guard let image = CIImage(mtlTexture: self, options: options) else { print("CIImage not created") return nil } guard let imageSrgb = image.matchedToWorkingSpace(from: CGColorSpace(name: CGColorSpace.linearSRGB)!) else { print("CIImage not converted to srgb") return nil } let flipped = imageSrgb.transformed(by: CGAffineTransform(scaleX: 1, y: -1)) return ctx.createCGImage(flipped, from: flipped.extent) } }
This code crashes on line 14 with EXC_BAD_ACCESS code 1 without additional calls. However, when I set options to nil in CIImage call on line 5, it renders fine.
FWIW, there's no change if I remove lines 9-13 and call createCGImage straight from image.
It also crashes, if I instantiate the context without the device:
return CIContext().createCGImage(...)
What am I missing?
Thanks. It probably was it, but I've found an even neater solution:
Code Block swift public var cgImage: CGImage? { guard let image = CIImage(mtlTexture: self, options: nil) else { print("CIImage not created") return nil } let flipped = image.transformed(by: CGAffineTransform(scaleX: 1, y: -1)) return CIContext().createCGImage(flipped, from: flipped.extent, format: CIFormat.RGBA8, colorSpace: CGColorSpace(name: CGColorSpace.linearSRGB)!) }