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

前端 未结 11 1886
自闭症患者
自闭症患者 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:25

    You could simply use UIVisualEffect to achieve blur effect. As you trying to achieve a blur effect using CoreImage.Try below code after import CoreImage to your class.

    var context = CIContext(options: nil)
    
    func blurEffect() {
    
        let currentFilter = CIFilter(name: "CIGaussianBlur") 
        let beginImage = CIImage(image: bg.image!)
        currentFilter!.setValue(beginImage, forKey: kCIInputImageKey)
        currentFilter!.setValue(10, forKey: kCIInputRadiusKey)
    
        let cropFilter = CIFilter(name: "CICrop")
        cropFilter!.setValue(currentFilter!.outputImage, forKey: kCIInputImageKey)
        cropFilter!.setValue(CIVector(cgRect: beginImage!.extent), forKey: "inputRectangle")
    
        let output = cropFilter!.outputImage 
        let cgimg = context.createCGImage(output!, from: output!.extent)
        let processedImage = UIImage(cgImage: cgimg!)
        bg.image = processedImage
    }
    

    Output:

    Note: I recommend you to test the code in real device as Simulator performance is too slow on coreImage.

    0 讨论(0)
  • 2020-12-28 17:27

    You can add blur effect by UIBlurEffect and UIVisualEffectView:

    @IBAction func blur(_ sender: Any) {
    
        let darkBlur = UIBlurEffect(style: UIBlurEffectStyle.dark) 
        let blurView = UIVisualEffectView(effect: darkBlur)
        blurView.frame = bg_imagview.bounds
        blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        bg_imagview.addSubview(blurView)
    
    }
    
    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2020-12-28 17:32

    for those who ❤️ protocols

    protocol Bluring {
        func addBlur(_ alpha: CGFloat)
    }
    
    extension Bluring where Self: UIView {
        func addBlur(_ alpha: CGFloat = 0.5) {
            // create effect
            let effect = UIBlurEffect(style: .dark)
            let effectView = UIVisualEffectView(effect: effect)
    
            // set boundry and alpha
            effectView.frame = self.bounds
            effectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            effectView.alpha = alpha
    
            self.addSubview(effectView)
        }
    }
    
    // Conformance
    extension UIView: Bluring {}
    
    // use
    someImageview.addBlur()
    
    0 讨论(0)
  • 2020-12-28 17:33

    Here is how I got my expected result in SWIFT 3.1: Hope it will help.

    func blurImage(image:UIImage) -> UIImage? {
            let context = CIContext(options: nil)
            let inputImage = CIImage(image: image)
            let originalOrientation = image.imageOrientation
            let originalScale = image.scale
    
            let filter = CIFilter(name: "CIGaussianBlur")
            filter?.setValue(inputImage, forKey: kCIInputImageKey)
            filter?.setValue(10.0, forKey: kCIInputRadiusKey) 
            let outputImage = filter?.outputImage
    
            var cgImage:CGImage?
    
            if let asd = outputImage
            {
                cgImage = context.createCGImage(asd, from: (inputImage?.extent)!)
            }
    
            if let cgImageA = cgImage
            {
                return UIImage(cgImage: cgImageA, scale: originalScale, orientation: originalOrientation)
            }
    
            return nil
        }
    
    0 讨论(0)
  • 2020-12-28 17:35

    I made the blur in a NSObject class, so I can use this method in whole project easily.

    class Helper: NSObject
    {
        class func addBlurView(_ inView : UIView) -> UIVisualEffectView
        {
            let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
            let blurEffectView = UIVisualEffectView(effect: blurEffect)
    
            //always fill the view
            blurEffectView.frame = inView.bounds
            blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            blurEffectView.alpha = 0.5
    
            return blurEffectView
        }
    }
    

    In ViewController I made object of UIVisualEffectView. Then call the helper class method of to add blur.

    import UIKit
    
    class ViewController: UIViewController
    {
       var blurEffectView : UIVisualEffectView!
        override func viewDidLoad() {
            super.viewDidLoad()
    
          blurEffectView = Helper.addBlurView((imgView)!)
          self.imgView.addSubview(blurEffectView)
    }
    
    0 讨论(0)
提交回复
热议问题