CIAdditionCompositing 段错误
CIAdditionCompositing seg faults
我正在尝试根据 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)
}
您在 combinedParameters
中使用 UIImage
而不是 CIImage
,并且 UIImage
没有 extent
方法,因此您遇到了崩溃。您需要使用在当前图像顶部创建的 CIImage
,如下所示,
let combinedParameters = [
"inputImage": blurred,
"inputBackgroundImage": backingCi,
"inputMaskImage": maskedImage
]
我正在尝试根据 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)
}
您在 combinedParameters
中使用 UIImage
而不是 CIImage
,并且 UIImage
没有 extent
方法,因此您遇到了崩溃。您需要使用在当前图像顶部创建的 CIImage
,如下所示,
let combinedParameters = [
"inputImage": blurred,
"inputBackgroundImage": backingCi,
"inputMaskImage": maskedImage
]