代码之家  ›  专栏  ›  技术社区  ›  Gargoyle

附加组合SEG故障

  •  0
  • Gargoyle  · 技术社区  · 5 年前

    我正在尝试根据 Apple's instructions 但一旦我尝试得到 outputImage 从ciadditioncompositing过滤器中,我得到一个SEG错误。这是我在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)
    }
    
    1 回复  |  直到 5 年前
        1
  •  0
  •   Kamran    5 年前

    你正在使用 UIImage 而不是 CIImage 在里面 combinedParameters 紫外图像 没有 extent 方法使您获得崩溃。你需要使用 图像 在当前图像的顶部创建,如下所示,

    let combinedParameters = [
        "inputImage": blurred,
        "inputBackgroundImage": backingCi,
        "inputMaskImage": maskedImage
    ]