I have scoured the internet for a better way to deal with this issue, Ofcourse i have found couple of bits and pieces here and there but not exactly what i was looking for so here is an easy one to use with the Swift-5 support.
extension UIImage {
var negative: UIImage? {
let context = CIContext(options: nil)
guard let currentFilter = CIFilter(name: "CIColorInvert") else { return nil }
currentFilter.setValue(CIImage(image: self), forKey: kCIInputImageKey)
if let output = currentFilter.outputImage,
let cgImage = context.createCGImage(output, from: output.extent) {
return UIImage(cgImage: cgImage, scale: scale, orientation: imageOrientation)
}
return nil
}
}
and ofcourse the usage willbe easy image.negative
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.