The setup is simple.
- A ViewController with UIImageView that has an image assigned.
- A UIButton that when clicked blurs the image in the UIImageView.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var bg: UIImageView!
@IBAction func blur(_ sender: Any) {
let inputImage = CIImage(cgImage: (bg.image?.cgImage)!)
let filter = CIFilter(name: "CIGaussianBlur")
filter?.setValue(inputImage, forKey: "inputImage")
filter?.setValue(10, forKey: "inputRadius")
let blurred = filter?.outputImage
bg.image = UIImage(ciImage: blurred!)
}
}
When the button is clicked the screen just turns white. Can't figure out what I'm doing wrong. Anyone know what I'm doing wrong?
You could simply use
UIVisualEffect
to achieve blur effect. As you trying to achieve a blur effect using CoreImage.Try below code afterimport CoreImage
to your class.Output:
Note: I recommend you to test the code in real device as Simulator performance is too slow on coreImage.