How to blur an existing image in a UIImageView with Swift?

前端 未结 11 1931
自闭症患者
自闭症患者 2020-12-28 16:48

The setup is simple.

  • A ViewController with UIImageView that has an image assigned.
  • A UIButton that when clicked blurs the image in the UIImageView.<
11条回答
  •  粉色の甜心
    2020-12-28 17:28

    There is actually a handy implementation right in CoreImage https://developer.apple.com/documentation/coreimage/ciimage/1645897-applyinggaussianblur

    extension UIImage {
    
    func blur(_ radius: Double) -> UIImage? {
        if let img = CIImage(image: self) {
            return UIImage(ciImage: img.applyingGaussianBlur(sigma: radius))
        }
        return nil
    }
    

提交回复
热议问题