我试图在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
        */
    }
}

但是应用程序始终会因下一个错误而崩溃:

ios - Swift中ColorMatrix的亮度,饱和度和对比度-LMLPHP

先感谢您!

最佳答案

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的初始化程序放在Extension方法中,因此您不必为其保留引用。

ios - Swift中ColorMatrix的亮度,饱和度和对比度-LMLPHP

ios - Swift中ColorMatrix的亮度,饱和度和对比度-LMLPHP

ios - Swift中ColorMatrix的亮度,饱和度和对比度-LMLPHP

希望能有所帮助。

关于ios - Swift中ColorMatrix的亮度,饱和度和对比度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60140691/

10-10 03:57