I have an image on which I applied blur effect using the following code:
@IBAction func blurSliderSlides(_ sender: UISlider) {
let currentValue = Int(sender.value)
let currentFilter = CIFilter(name: "CIGaussianBlur")
currentFilter!.setValue(CIImage(image: mainImage), forKey: kCIInputImageKey)
currentFilter!.setValue(currentValue, forKey: kCIInputRadiusKey)
let cropFilter = CIFilter(name: "CICrop")
cropFilter!.setValue(currentFilter!.outputImage, forKey: kCIInputImageKey)
cropFilter!.setValue(CIVector(cgRect: (CIImage(image: mainImage)?.extent)!), forKey: "inputRectangle")
let output = cropFilter!.outputImage
let cgimg = context.createCGImage(output!, from: output!.extent)
processedImage = UIImage(cgImage: cgimg!)
backgroundImage.image = processedImage
}
Now I have three buttons square circle and rectangle. when ever use tap any of button that type of view is created on mid of blurred image and user can make that view large or small using slider. but all I want when subview is added of any shape the background image should be unblur from that point where view is created.
subview created on background image:
@IBAction func squareButtonTapped(_ sender: Any) {
squareView.removeFromSuperview()
squareView.frame = CGRect(x: backgroundImage.bounds.midX, y: backgroundImage.bounds.midY, width: CGFloat(backgroundImage.frame.size.height * 20 / 100), height: CGFloat(backgroundImage.frame.size.width * 10 / 100))
backgroundImage.addSubview(squareView)
}
using slider subview can be changed:
@IBAction func blurTypeSliderSlides(_ sender: UISlider) {
squareView.transform = CGAffineTransform(scaleX: CGFloat(sender.value / 10), y: CGFloat(sender.value / 10))
}
how to remove blur effect from background image at point where subview is added. I have searched a lot but nothing find helpful as my requirement. can someone please help. Thanks in advance.
It looks like you have a
UIImageand aUIImageViewdeclared as a property of your controller... and inviewDidLoad()you're loading that image, something like:I'm guessing that, because in your
func blurSliderSlides(_ sender: UISlider)you have this line:and at the end:
So, when you add the "shape" subview, set the
.imageto the original:Edit - after clarification in comments...
You don't want to think in terms of "adding subviews."
Instead, use two image views... one containing the original image, and another containing the blurred image, overlaid on top. Then use a layer mask (with a "hole cut") on the blurred image view to let the original "show through."
So,
And it can look like this at run-time:
Here is some example code you can try out. It has one slider which controls the "cut-out oval" as a percentage of the image view width:
You should have no trouble using the
updateBlurMask()func as a basis for your other shapes.