I need to apply a CIGaussianBlur filter only on a small part of an image like this:
Is there any way to do this? For example when I apply the filter perhaps I think that I have to specify the dimension of the filter (someone like CGRectMake).
var imageToBlur = CIImage(image: coro.logo)
var blurfilter = CIFilter(name: "CIGaussianBlur")
blurfilter.setValue(imageToBlur, forKey: "inputImage")
blurfilter.setValue(2, forKey: "inputRadius")
var resultImage = blurfilter.valueForKey("outputImage") as! CIImage
var blurredImage = UIImage(CIImage: resultImage)
self.immagineCoro.image = blurredImage
The idea is to make a new image from a rect in the source image, blur this new image then composite it back into the source.
I'm making this an extension to UIImage for an easier usage.
extension UIImage {
func getImageFromRect(rect: CGRect) -> UIImage? {
if let cg = self.CGImage,
let mySubimage = CGImageCreateWithImageInRect(cg, rect) {
return UIImage(CGImage: mySubimage)
}
return nil
}
func blurImage(withRadius radius: Double) -> UIImage? {
let inputImage = UIKit.CIImage(CGImage: self.CGImage!)
if let filter = CIFilter(name: "CIGaussianBlur") {
filter.setValue(inputImage, forKey: kCIInputImageKey)
filter.setValue((radius), forKey: kCIInputRadiusKey)
if let blurred = filter.outputImage {
return UIImage(CIImage: blurred)
}
}
return nil
}
func drawImageInRect(inputImage: UIImage, inRect imageRect: CGRect) -> UIImage {
UIGraphicsBeginImageContext(self.size)
self.drawInRect(CGRectMake(0.0, 0.0, self.size.width, self.size.height))
inputImage.drawInRect(imageRect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
func applyBlurInRect(rect: CGRect, withRadius radius: Double) -> UIImage? {
if let subImage = self.getImageFromRect(rect),
let blurredZone = subImage.blurImage(withRadius: radius) {
return self.drawImageInRect(blurredZone, inRect: rect)
}
return nil
}
}
Usage:
// your image
let sourceImage = UIImage(...)
// the rect to blur
let targetZone = CGRectMake(-50, sourceImage.size.height - 220, sourceImage.size.width + 100, 220)
// apply our functions to the source image
if let resultImage = sourceImage.applyBlurInRect(targetZone, withRadius: 6.0) {
// use resultImage
}
Before:
After:
Try to use this
func blurImage(image:UIImage, forRect rect: CGRect) -> UIImage?
{
let context = CIContext(options: nil)
let inputImage = CIImage(CGImage: image.CGImage!)
let filter = CIFilter(name: "CIGaussianBlur")
filter?.setValue(inputImage, forKey: kCIInputImageKey)
filter?.setValue((70.0), forKey: kCIInputRadiusKey)
let outputImage = filter?.outputImage
var cgImage:CGImageRef?
if let asd = outputImage
{
cgImage = context.createCGImage(asd, fromRect: rect)
}
if let cgImageA = cgImage
{
return UIImage(CGImage: cgImageA)
}
return nil
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With