Blur UIImage to achieve effect like passcode blur of the wallpaper with swift

▼魔方 西西 提交于 2019-12-04 19:27:53

As you only need to support iOS 8 and above you can use the new UIVisualEffectView, which is the new built-in way of adding blur.

It's very easy to use, something like the following:

let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = myFrame
self.view.addSubview(blurView)

You can see that you can specifiy a UIBlurEffectStyle, which will allow you to make it darker as well.

Try this to blur and crop image

func applyBlurEffect(image: UIImage){
var imageToBlur = CIImage(image: image)
var blurfilter = CIFilter(name: "CIGaussianBlur")
blurfilter.setValue(5, forKey: kCIInputRadiusKey)
blurfilter.setValue(imageToBlur, forKey: "inputImage")
var resultImage = blurfilter.valueForKey("outputImage") as! CIImage
var blurredImage = UIImage(CIImage: resultImage)
var cropped:CIImage=resultImage.imageByCroppingToRect(CGRectMake(0, 0,imageToBlur.extent().size.width, imageToBlur.extent().size.height))
blurredImage = UIImage(CIImage: cropped)
self.backgroundImage.image = blurredImage
}

I tried updating the answer but it was nonsensically denied. Here's the latest syntax:

Swift 3.1

let blurEffect = UIBlurEffect(style: .dark)
let blurView = UIVisualEffectView(effect: blurEffect)
self.view.addSubview(blurView)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!