iosswiftcifilterimage-editing

How can i remove existing CIFilter from an image in iOS?


// MARK: - Remove filter from image
func removeFilterFromImage(image: UIImage) -> UIImage {
    // Convert UIImage to CIImage
    guard let ciInput = CIImage(image: image) else {
        return image
    }

// Create a neutral filter (e.g., identity filter)
let neutralFilter = CIFilter(name: "CISRGBToneCurveToLinear")

// Set the input image for the filter
neutralFilter?.setValue(ciInput, forKey: kCIInputImageKey)

// Get the output CIImage from the filter
guard let ciOutput = neutralFilter?.outputImage else {
    return image
}

// Create a CIContext
let ciContext = CIContext()

// Render the output CIImage as a CGImage
guard let cgImage = ciContext.createCGImage(ciOutput, from: ciOutput.extent) else {
    return image
}

// Create a new UIImage from the CGImage
let outputImage = UIImage(cgImage: cgImage)

return outputImage

}

This is what i have tried, but it doesn't seems to be working. From the sources i read that it is not possible unless there is a reverse filter, so there should be reverse filter for every filter in that case, if not then how can i remove existing filter from an image eg: Gaussian Blur


Solution

  • Applying a CIFilter to an image is a destructive operation, meaning that it cannot be reverted to get back the original image.