Skip to content

Instantly share code, notes, and snippets.

@VojtaStruhar
Created October 2, 2019 08:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VojtaStruhar/058af1d95a97f41e8c9356e464201b2d to your computer and use it in GitHub Desktop.
Save VojtaStruhar/058af1d95a97f41e8c9356e464201b2d to your computer and use it in GitHub Desktop.
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