我正在写代码。这是我的完整源代码。
我确认我的代码在 Xcode 8.3.3 中成功运行。
但是,我的代码在 Xcode 9.x(9.1 和 9.2)中崩溃了。
确切地说,我的代码在“filter.setValue(inputCIImage, forKey: kCIInputImageKey)”和“NSException (sig abrt)”中崩溃了
即使我使用了调试器,我也没有找到原因。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
let inputImage = UIImage(named: "testImage.png")
override func viewDidLoad() {
super.viewDidLoad()
let inputCIImage = CIImage(image: inputImage!)
let filter = CustomFilter()
filter.setValue(inputCIImage, forKey: kCIInputImageKey)
let outputImage = filter.outputImage!
let context = CIContext()
let outputCGImage = context.createCGImage(outputImage, from: outputImage.extent)
imageView.image = UIImage(cgImage: outputCGImage!)
}
}
class CustomFilter: CIFilter {
var inputImage: CIImage?
override public var outputImage: CIImage! {
get {
if let inputImage = self.inputImage {
let args = [inputImage as AnyObject]
return createCustomKernel().apply(extent: inputImage.extent, arguments: args)
} else {
return nil
}
}
}
func createCustomKernel() -> CIColorKernel {
let kernelString =
"kernel vec4 chromaKey(__sample s){\n" +
"vec4 newPixel = s.rgba;\n" +
"newPixel[0] = 0.0;\n" +
"newPixel[2] = newPixel[2] / 2.0;\n" +
"return newPixel;\n" + "}"
return CIColorKernel(source: kernelString)!
}
}