简体   繁体   中英

UIVisualEffectView blur image

I use UIVisualEffectView to blur my imageview. Than I use this code to convert it to image and save to image library.

 UIGraphicsBeginImageContextWithOptions(imageContainerView.bounds.size, true, 1)
 imageContainerView.drawHierarchy(in: imageContainerView.bounds, afterScreenUpdates: true)
 let newImage = UIGraphicsGetImageFromCurrentImageContext()!
 UIGraphicsEndImageContext()

imageContainerView is a view which contains imageview and UIVisualEffectView.

Before I convert imageContainerView to image I increase it's size to 2000x2000 so I will get high quality image, If I don't do this all images inside imageContainerView becomes pixelated.

The problem is that after converting imageContainerView to image the blurred image becomes almost not blurred because I increased size of container view.

Can you suggest a solution for this problem? I just want to get a blurred image so if you know other way to blur image using slider (the blur code should work fast so the UI will not freeze) please tell me.

I just want to get a blurred image

Ok you can implement some of your blur filter effects, like this one, and slider to it.

var context = CIContext(options: nil)

func blur() {

    let blurFilter = CIFilter(name: "CIGaussianBlur") 
    let beginImage = CIImage(image: YOURIMAGE!)
    blurFilter!.setValue(beginImage, forKey: kCIInputImageKey)
    blurFilter!.setValue(8, forKey: kCIInputRadiusKey)

    let cropFilter = CIFilter(name: "CICrop")
    cropFilter!.setValue(blurFilter!.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: cg cgImg mg!)
    bg.image = processedImage
}

Or there are multiple libraries on GitHub like this one eg.: https://github.com/FlexMonkey/Blurable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM