How to programmatically change the hue of UIImage?

Viewed 10604

I am very new to the image processing. I have to implement hue effect in my iPhone application project. Therefore, I need to change the hue of UIImage. Please provide me any sample code or tutorial link for this.

Thanks in advance

6 Answers

For Swift 5.0 updated from @JonLoard

 func changeHue(_ image: UIImage, hueValue:CGFloat)->UIImage?{
    if let sourceCGImage = image.cgImage{
        let sourceCore = CIImage(cgImage: sourceCGImage)
        // Apply a CIHueAdjust filter
        let hueAdjust = CIFilter(name: "CIHueAdjust")
        hueAdjust?.setDefaults()
        hueAdjust?.setValue(sourceCore, forKey: "inputImage")
        hueAdjust?.setValue(CGFloat(hueValue), forKey: "inputAngle")
        if let resultCore  = hueAdjust?.value(forKey: "outputImage") as? CIImage{
            let context = CIContext(options: nil)
            if let resultRef = context.createCGImage(resultCore, from: resultCore.extent){
                let result = UIImage(cgImage: resultRef,scale: 1,orientation: image.imageOrientation)
                return result
            }
        }
        
    }
    return image
}
Related