Why is CIFilter.differenceBlendMode behave differently from View's blendMode property?

31 views Asked by At
struct ContentView: View {
    
    var processed: CIImage {
        let filter = CIFilter.differenceBlendMode()
        filter.inputImage = UIImage(resource: .image).cgImage.map({CIImage(cgImage: $0)})
        filter.backgroundImage = UIImage(resource: .whiteBg).cgImage.map({CIImage(cgImage: $0)})
        return filter.outputImage!
    }
    
    var body: some View {
        VStack(spacing: 0) {
            Image(ciImage: processed)
                .resizable()
                .scaledToFit()
            Image(.image)
                .resizable()
                .scaledToFit()
                .blendMode(.difference)
        }
    }
}

enter image description here

This produces two different Images.

What's missing from the filter?

1

There are 1 answers

1
Kyle Fang On

It's a problem with colorspace

let context = CIContext(options: [
    .workingColorSpace: CGColorSpaceCreateDeviceRGB()
])

Fixed it