Created
October 2, 2019 08:11
-
-
Save VojtaStruhar/058af1d95a97f41e8c9356e464201b2d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func addFilter(filterType : FilterType) -> UIImage { | |
let filter = CIFilter(name: filterType.rawValue) | |
// convert UIImage to CIImage and set as input | |
let ciInput = CIImage(image: self) | |
filter?.setValue(ciInput, forKey: "inputImage") | |
if filterType == .Sharpen { | |
filter?.setValue(0.8, forKey: "inputIntensity") | |
filter?.setValue(8, forKey: "inputRadius") | |
} | |
if filterType == .Posterize { | |
filter?.setValue(15, forKey: "inputLevels") | |
} | |
if filterType == .MonoChrome { | |
filter?.setValue(CIColor(red: 0.8, green: 0.8, blue: 0.8), forKey: "inputColor") | |
filter?.setValue(1.0, forKey: "inputIntensity") | |
} | |
if filterType == .NoiseReduction { | |
filter?.setValue(2, forKey: "inputSharpness") | |
} | |
// get output CIImage, render as CGImage first to retain proper UIImage scale | |
let ciOutput = filter?.outputImage | |
let ciContext = CIContext() | |
let cgImage = ciContext.createCGImage(ciOutput!, from: (ciOutput?.extent)!) | |
return UIImage(cgImage: cgImage!, scale: 1, orientation: imageOrientation) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment