我正在尝试根据Apple's instructions创建一个倾斜移位滤波器,但是当我尝试从CIAdditionCompositing滤波器中获取outputImage
时,就会遇到段错误。这是我用作UIImage扩展的代码。我做错了什么事?
public func tiltShift() -> UIImage? {
guard let backingCi = ciImage ?? CIImage(image: self) else {
return nil
}
let blurred = backingCi.applyingGaussianBlur(sigma: 10)
var gradientParameters = [
"inputPoint0": CIVector(x: 0, y: 0.75 * size.height),
"inputColor0": CIColor(red: 0, green: 1, blue: 0, alpha: 1),
"inputPoint1": CIVector(x: 0, y: 0.5 * 10),
"inputColor1": CIColor(red: 0, green: 1, blue: 0, alpha: 0)
];
guard let inputGradient = CIFilter(name: "CILinearGradient", parameters: gradientParameters),
let inputImage = inputGradient.outputImage else {
return nil
}
gradientParameters["inputPoint0"] = CIVector(x: 0, y: 0.25 * size.height)
guard let backgroundGradient = CIFilter(name: "CILinearGradient", parameters: gradientParameters),
let backgroundImage = backgroundGradient.outputImage else {
return nil
}
let maskParameters = [
"inputImage": inputImage,
"inputBackgroundImage": backgroundImage
]
guard let mask = CIFilter(name: "CIAdditionCompositing", parameters: maskParameters),
let maskedImage = mask.outputImage else { // <=- This line aborts
return nil
}
let combinedParameters = [
"inputImage": blurred,
"inputBackgroundImage": self,
"inputMaskImage": maskedImage
]
guard let blend = CIFilter(name: "CIBlendWithMask", parameters: combinedParameters) else {
return nil
}
let context = CIContext(options: nil)
guard let blendImg = blend.outputImage,
let cgimg = context.createCGImage(blendImg, from: blendImg.extent) else { return nil }
return UIImage(cgImage: cgimg)
}
答案 0 :(得分:0)
您在UIImage
中使用的是CIImage
而不是combinedParameters
,并且UIImage
没有extent
方法,因此您会崩溃。您需要使用在当前图片的顶部创建的CIImage
,如下所示,
let combinedParameters = [
"inputImage": blurred,
"inputBackgroundImage": backingCi,
"inputMaskImage": maskedImage
]