快速添加模糊效果到背景

我正在设置一个背景图像来查看控制器。 但也是我想添加模糊效果到这个背景。 我怎样才能做到这一点?

我使用以下代码设置背景:

self.view.backgroundColor = UIColor(patternImage: UIImage(named: "testBg")!) 

我在互联网上find模糊imageview我怎么能实现这个到我的背景?

 var darkBlur = UIBlurEffect(style: UIBlurEffectStyle.Dark) // 2 var blurView = UIVisualEffectView(effect: darkBlur) blurView.frame = imageView.bounds // 3 imageView.addSubview(blurView) 

我已经testing了这个代码,它的工作正常:

 let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark) let blurEffectView = UIVisualEffectView(effect: blurEffect) blurEffectView.frame = view.bounds blurEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] view.addSubview(blurEffectView) 

对于swift 3.0:

 let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark) let blurEffectView = UIVisualEffectView(effect: blurEffect) blurEffectView.frame = view.bounds blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] view.addSubview(blurEffectView) 

在这里你可以看到结果:

在这里输入图像说明

或者你可以使用这个库:

https://github.com/FlexMonkey/Blurable

你可以做一个UIImageView的扩展。

Swift 2.0

 import Foundation import UIKit extension UIImageView { func makeBlurImage(targetImageView:UIImageView?) { let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark) let blurEffectView = UIVisualEffectView(effect: blurEffect) blurEffectView.frame = targetImageView!.bounds blurEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] // for supporting device rotation targetImageView?.addSubview(blurEffectView) } } 

用法:

 override func viewDidLoad() { super.viewDidLoad() let sampleImageView = UIImageView(frame: CGRectMake(0, 200, 300, 325)) let sampleImage:UIImage = UIImage(named: "ic_120x120")! sampleImageView.image = sampleImage //Convert To Blur Image Here sampleImageView.makeBlurImage(sampleImageView) self.view.addSubview(sampleImageView) } 

Swift 3扩展

 import Foundation import UIKit extension UIImageView { func addBlurEffect() { let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light) let blurEffectView = UIVisualEffectView(effect: blurEffect) blurEffectView.frame = self.bounds blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] // for supporting device rotation self.addSubview(blurEffectView) } } 

用法:

 yourImageView.addBlurEffect() 

@AlvinGeorge应该只使用:

 extension UIImageView{ func blurImage() { let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark) let blurEffectView = UIVisualEffectView(effect: blurEffect) blurEffectView.frame = self.bounds blurEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] // for supporting device rotation self.addSubview(blurEffectView) } } 

用法

  blurredBackground.frame = self.view.bounds blurredBackground.blurImage() self.view.addSubview(self.blurredBackground) 

对于Swift 3(iOS 10.0和8.0)

 var darkBlur:UIBlurEffect = UIBlurEffect() if #available(iOS 10.0, *) { //iOS 10.0 and above darkBlur = UIBlurEffect(style: UIBlurEffectStyle.prominent)//prominent,regular,extraLight, light, dark } else { //iOS 8.0 and above darkBlur = UIBlurEffect(style: UIBlurEffectStyle.dark) //extraLight, light, dark } let blurView = UIVisualEffectView(effect: darkBlur) blurView.frame = self.view.frame //your view that have any objects blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight] view.addSubview(blurView) 

U还可以使用CoreImage来创build带有黑暗效果的模糊图像

  1. 为图像创build快照

     func snapShotImage() -> UIImage { UIGraphicsBeginImageContext(self.frame.size) if let context = UIGraphicsGetCurrentContext() { self.layer.renderInContext(context) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } return UIImage() } 
  2. 像你所希望的那样应用CoreImagefilter

     private func bluredImage(view:UIView, radius:CGFloat = 1) -> UIImage { let image = view.snapShotImage() if let source = image.CGImage { let context = CIContext(options: nil) let inputImage = CIImage(CGImage: source) let clampFilter = CIFilter(name: "CIAffineClamp") clampFilter?.setDefaults() clampFilter?.setValue(inputImage, forKey: kCIInputImageKey) if let clampedImage = clampFilter?.valueForKey(kCIOutputImageKey) as? CIImage { let explosureFilter = CIFilter(name: "CIExposureAdjust") explosureFilter?.setValue(clampedImage, forKey: kCIInputImageKey) explosureFilter?.setValue(-1.0, forKey: kCIInputEVKey) if let explosureImage = explosureFilter?.valueForKey(kCIOutputImageKey) as? CIImage { let filter = CIFilter(name: "CIGaussianBlur") filter?.setValue(explosureImage, forKey: kCIInputImageKey) filter?.setValue("\(radius)", forKey:kCIInputRadiusKey) if let result = filter?.valueForKey(kCIOutputImageKey) as? CIImage { let bounds = UIScreen.mainScreen().bounds let cgImage = context.createCGImage(result, fromRect: bounds) let returnImage = UIImage(CGImage: cgImage) return returnImage } } } } return UIImage() } 

find另一种方式..我使用苹果的UIImage + ImageEffects。

  UIImage *effectImage = [image applyExtraLightEffect]; self.imageView.image = effectImage;