【问题标题】:Brightness, saturation and contrast with ColorMatrix in SwiftSwift 中 ColorMatrix 的亮度、饱和度和对比度
【发布时间】:2020-05-25 04:45:32
【问题描述】:

我正在尝试在 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
  • 是的,它是 nil,但我不知道为什么......我看不出哪里失败了。

标签: ios swift xcode cifilter colormatrix


【解决方案1】:

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 方法中,这样您就不必为它保留引用。

希望对您有所帮助。

【讨论】:

  • 您好 Arashk,感谢您的回答!我把你提到的代码放在了调试区域,但现在出现错误:Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<CIColorMatrix 0x600002acbf80> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key inputRvector.'
  • 代码在操场上运行良好。我对此进行了测试,但似乎问题出在 setValue 我更新了答案,检查并告诉我。如果出现错误,请在动态之前添加 @objc 并再次检查它应该可以解决问题。
  • 我首先在没有@objc 的情况下进行了测试,并出现了同样的错误,当我尝试添加@objc 时,在这一行出现错误:@objc can only be used with members of classes, @objc protocols, and concrete extensions of classes
  • 代码工作正常我也在模拟器上测试它们我更新答案,将图像添加到你的项目文件夹并测试它。
  • 伙计,你不会相信最后一个错误是从哪里来的...我写的是forKey: "inputRvector"而不是forKey: "inputRVector"...大写字母!!我觉得自己完全傻了,哈哈哈!无论如何@Arashk,非常感谢你的所有努力,因为它对我真的很有帮助。
猜你喜欢
  • 2019-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-01
  • 1970-01-01
  • 2011-02-13
相关资源
最近更新 更多