ColorMatrix 的亮度、饱和度和对比度 Swift

Brightness, saturation and contrast with ColorMatrix in Swift

我正在尝试在 Swift 中重新编写我的 Android 应用程序以在 iOS 中启动,但我遇到了瓶颈...

Android 应用程序是关于基于 ColorMatrix 求和的图像处理......即下一个矩阵来制作灰度图像:

float[] bwMatrix = {
                0f, 1f, 0f, 0f, 0f,
                0f, 1f, 0f, 0f, 0f,
                0f, 1f, 0f, 0f, 0f,
                0f, 0f, 0f, 1f, 0f,
                0f, 0f, 0f, 0f, 1f};

现在,尝试应用与 Swift 相同的概念,我的代码如下:

import UIKit

dynamic var colorMatrix : CIFilter?

class editionViewController: UIViewController {

    @IBOutlet weak var editionImageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let fileManager = FileManager.default
        let imagePath = (self.getDirectoryPath() as NSURL).appendingPathComponent("editionImg0.png")
        let urlString: String = imagePath!.absoluteString
        if fileManager.fileExists(atPath: urlString) {
            let originalImage = UIImage (contentsOfFile: urlString)
            let liaImage = originalImage?.liaFilter()
            editionImageView.image = liaImage
        }
    }

    func getDirectoryPath() -> NSURL {
        let path = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("tempImages")
        let url = NSURL (string: path)
        return url!
    }
}

extension UIImage {
    //Lia filter
    func liaFilter() -> UIImage? {
        let inImage = CIImage(image: self)
        colorMatrix?.setDefaults()
        colorMatrix?.setValue(inImage, forKey: "inputImage")
        colorMatrix?.setValue(CIVector(x: 0, y: 1, z: 0, w: 0), forKey: "inputRvector")
        colorMatrix?.setValue(CIVector(x: 0, y: 1, z: 0, w: 0), forKey: "inputGvector")
        colorMatrix?.setValue(CIVector(x: 0, y: 1, z: 0, w: 0), forKey: "inputBvector")
        colorMatrix?.setValue(CIVector(x: 0, y: 0, z: 0, w: 1), forKey: "inputAvector")
        let outCIImage = (colorMatrix?.outputImage)
        return UIImage (ciImage: outCIImage!) //HERE is the crash
        /*
        if let outCIImage = colorMatrix?.outputImage {
            return UIImage (ciImage: outCIImage)
        }
        return nil
        */
    }
}

但应用程序总是崩溃并出现下一个错误:

提前致谢!

colorMatrix 属性 是 nil 因为您从未正确初始化它。

从您的控制器中删除 dynamic var colorMatrix: CIFilter? 并使用下面的代码。

class editionViewController: UIViewController {

    @IBOutlet weak var editionImageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        if let image = UIImage(named: "myImage.png") {
            editionImageView.image = image.liaFilter()
        }
    }
}

extension UIImage {
    //Lia filter
    func liaFilter() -> UIImage {
        let inImage = CIImage(image: self)

        let rgbVector = CIVector(x: 0, y: 1, z: 0, w: 0)
        let aVector = CIVector(x: 0, y: 0, z: 0, w: 1)

        dynamic let colorMatrix = CIFilter(name: "CIColorMatrix")

        if colorMatrix != nil {
            colorMatrix?.setDefaults()
            colorMatrix?.setValue(inImage, forKey: kCIInputImageKey)
            colorMatrix?.setValue(rgbVector, forKey: "inputRVector")
            colorMatrix?.setValue(rgbVector, forKey: "inputGVector")
            colorMatrix?.setValue(rgbVector, forKey: "inputBVector")
            colorMatrix?.setValue(aVector, forKey: "inputAVector")

            if let output = colorMatrix?.outputImage, let cgImage = CIContext().createCGImage(output, from: output.extent) {
                return UIImage(cgImage: cgImage)
            }
        }

        return self
    }
}

我修复了你的 UIImage 扩展方法。

我将 CIFilter 的初始化程序放在扩展方法中,这样您就不必为它保留引用。

希望对您有所帮助。