首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift中亮度、饱和度及与ColorMatrix的对比

Swift中亮度、饱和度及与ColorMatrix的对比
EN

Stack Overflow用户
提问于 2020-02-09 19:52:20
回答 1查看 928关注 0票数 1

我正试图用Swift重新编写我的安卓应用程序,以便为iOS启动,我已经到了墙边.

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

代码语言:javascript
复制
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中应用相同的概念,我的代码如下:

代码语言:javascript
复制
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
        */
    }
}

但是app总是在下一个错误中崩溃:

提前谢谢你!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-10 01:03:22

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

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

代码语言:javascript
复制
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的初始化器放在扩展方法中,这样您就不必保存它的引用了。

希望能帮上忙。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60140691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档