Nikolay Nikolaenko
Nikolay Nikolaenko

Reputation: 77

UIView.transition animation It only works once, a second time the picture just disappears

How to make the animation consistent?

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
}

And for Button:

@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)
}

Upvotes: 1

Views: 190

Answers (1)

Marco Boerner
Marco Boerner

Reputation: 1556

You need to change your function a bit to the following and it should work. The problem is the conversion from a ciImage to a UIImage without using context. See here:

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

and here:

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
        }

Upvotes: 1

Related Questions