I am attempting to output a CIImage to disk, and am looking for the most efficient way possible. I found this question, which suggests using the writeJPEGRepresentation
/writePNGRepresentation
methods of CIContext. However, whenever I attempt to use those methods, they produce blank images.
In the case of JPEG, it produces a pure black image. PNG produces a pure transparent image. The HEIF method also produced a pure black image. Same result if I use the jpegRepresentation
or pngRepresentation
methods which generate a Data
, instead of writing directly to disk.
If I instead render these objects using createCGImage
, and then write out the data from the CGImage, they look fine.
Any ideas why this might be happening?
Here's a simple example that illustrates the problem:
let ciImage = CIImage(contentsOf: photoURL, options: [CIImageOption.applyOrientationProperty: true])!
let context = CIContext()
try! context.writeJPEGRepresentation(of: ciImage, to: FileManager.default.temporaryDirectory.appendingPathComponent("testFromCIImage.jpeg"), colorSpace: CGColorSpace(name: CGColorSpace.sRGB)!, options: [:])
let cgImage: CGImage = context.createCGImage(ciImage, from: ciImage.extent)!
let uiImage = UIImage(cgImage: cgImage)
try! uiImage.jpegData(compressionQuality: 1.0)!.write(to: FileManager.default.temporaryDirectory.appendingPathComponent("testFromUIImage.jpeg"))
testFromCIImage.jpeg
appears as a blank image, with the same size as the original input image. testFromUIImage.jpeg
appears identical to the input image.
I have created a sample project that illustrates the issue, along with a few sample images.
Update
I have found only one condition where the image outputs correctly - when the source image is a PNG, and the colorSpace
parameter of writeJPEGRepresentation
matches the color space of the source image. I have not had success with any JPEG, even when the color space matches.