How to programmatically change the hue of UIImage?

后端 未结 5 881
南笙
南笙 2020-12-01 01:26

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 p

5条回答
  •  余生分开走
    2020-12-01 01:47

    Swift version of Dondragmer's rotating code above

    // Convert the filter output back into a UIImage.
    // This section from http://stackoverflow.com/a/7797578/1318452
    func imageWithImage(source: UIImage, rotatedByHue: CGFloat) -> UIImage {
        // Create a Core Image version of the image.
        let sourceCore = CIImage(CGImage: source.CGImage)
        // Apply a CIHueAdjust filter
        let hueAdjust = CIFilter(name: "CIHueAdjust")
        hueAdjust.setDefaults()
        hueAdjust.setValue(sourceCore, forKey: "inputImage")
        hueAdjust.setValue(CGFloat(rotatedByHue), forKey: "inputAngle")
        let resultCore  = hueAdjust.valueForKey("outputImage") as CIImage!
        let context = CIContext(options: nil)
        let resultRef = context.createCGImage(resultCore, fromRect: resultCore!.extent())
        let result = UIImage(CGImage: resultRef)
        return result!
    }
    

提交回复
热议问题