CIMaskToAlpha doesn't retain actual hardness (blurriness)

48 Views Asked by At

I am trying to write a function that will convert all black image pixels of drawn line into clear with mantaining their hardness (blurriness). For getting the result I have used CIMaskToAlpha filter, but it makes the line thicker than the actual lined image. Here is my code, actual image, and output image. I'm looking for a solution that will allow me to convert the black pixels of the line in the image into clear pixel maintaining their hardness without thickening the line. What modifications or alternative approach should I consider to achieve the desired output?

Comparison

extension UIImage {
    func cleared() -> UIImage {
        let entryImage = self
        let context = CIContext()
        if let cgImage = entryImage.cgImage {
            let image = CIImage(cgImage: cgImage)
            if let filter = CIFilter(name: "CIMaskToAlpha") {
                filter.setDefaults()
                filter.setValue(image, forKey: kCIInputImageKey)
                if let result = filter.outputImage {
                    if let cgImageResult = context.createCGImage(result, from: result.extent) {
                        let newImage = UIImage(cgImage: cgImageResult, scale: entryImage.scale, orientation: .up)
                        return newImage
                    }
                }
            }
        }
        return self
    }
}
0

There are 0 best solutions below