简体   繁体   English

如何使用 Swift 创建反转颜色(图像的负片)UIImage

[英]How to create an inverted colour (Negative of an image) UIImage using Swift

For reference:以供参考:

The left image would be the end result of the one on the right.左侧图像将是右侧图像的最终结果。

在此处输入图像描述

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM