UIView.transition animation只工作一次,第二次图片消失

问题描述 投票:1回答:1

如何使动画保持一致?

func addBlurTo(_ image: UIImage) -> UIImage? {

    guard let ciImg = CIImage(image: image) else { return nil }
    let blur = CIFilter(name: "CIGaussianBlur")
    blur?.setValue(ciImg, forKey: kCIInputImageKey)
    blur?.setValue(1 , forKey: kCIInputRadiusKey)
    if let outputImg = blur?.outputImage {
        return UIImage(ciImage: outputImg)
    }
    return nil
}

对于按钮:

@IBAction func Button7(_ sender: UIButton) {

        UIView.transition(with: imageView, duration: 1, options: .transitionCrossDissolve, animations: {
            guard let testImage = self.imageView.image else {return}
            self.imageView.image = self.addBlurTo(testImage)
        }, completion: nil)
}
ios swift uiviewanimationtransition
1个回答
0
投票

您需要将功能更改为以下内容,并且应该可以使用。问题是从ciImage到UIImage的转换而不使用上下文。看到这里:

If the UIImage object was initialized using a CIImage object, the value of the property is NULL.

和这里:

Generating qr code with swiftui shows empty picture

func addBlurTo(_ image: UIImage) -> UIImage? {

            guard let ciImg = CIImage(image: image) else { return nil }
            let blur = CIFilter(name: "CIGaussianBlur")
            blur?.setValue(ciImg, forKey: kCIInputImageKey)
            blur?.setValue(1 , forKey: kCIInputRadiusKey)
            if let outputImg = blur?.outputImage {
                let context = CIContext()
                guard let cgOutput = context.createCGImage(outputImg, from: outputImg.extent) else { return nil }
                return UIImage(cgImage: cgOutput)
            }
            return nil
        }
© www.soinside.com 2019 - 2024. All rights reserved.