gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:00:37 28 4
gpt4 key购买 nike

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

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

enter image description here

先感谢您!

最佳答案

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

删除 dynamic var colorMatrix: CIFilter?从您的 Controller 并使用下面的代码。

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

image if project

ViewController

enter image description here

希望这有帮助。

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com