[英]How to create an inverted colour (Negative of an image) UIImage using Swift
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.我已经在互联网上搜索了一个更好的方法来处理这个问题,当然我在这里和那里找到了一些零碎的东西,但不完全是我想要的,所以这里是一个易于使用的 Swift-5 支持。
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
当然使用起来很容易image.negative
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.