Thank you!
The color spaces are identical.
Before:
<CGColorSpace 0x281da9440> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; QuickTime 'nclc' Video (1,1,6))
After:
<CGColorSpace 0x281da9440> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; QuickTime 'nclc' Video (1,1,6))
But you are absolutely right, the code I posted was wrong!
Here is a cleaner version that seems to work
let pixelBufferRef = frame.capturedImage
let resolution = frame.camera.imageResolution
var image = CIImage(cvPixelBuffer: pixelBufferRef)
let viewportSize = CGSize(width: resolution.height, height: resolution.width)
let transform = frame.displayTransform(for: .portraitUpsideDown, viewportSize: viewportSize)
image = image.transformed(by: transform)
do {
let context = CIContext()
let url = getDocumentsDirectory().appending(component: "captured-image-corrected.png")
try context.writePNGRepresentation(of: image, to: url, format: .RGBA8, colorSpace: image.colorSpace!)
} catch {
fatalError(error.localizedDescription)
}
Does this sound better to you?