-4

I've been searching for this, but i was not able to find a correct answer.

I wanna make a button, and if you press on this button, an image view will be blurred with a gaussian blur.

How can i do that?

user2291669
  • 37
  • 1
  • 4

2 Answers2

8

You could use StackBluriOS or GPUImage

or

Try this (found here): Answer from Stackoverflow

@interface UIImage (ImageBlur)
- (UIImage *)imageWithGaussianBlur;
@end

@implementation UIImage (ImageBlur)
- (UIImage *)imageWithGaussianBlur {
    float weight[5] = {0.2270270270, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162};
    // Blur horizontally
    UIGraphicsBeginImageContext(self.size);
    [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[0]];
    for (int x = 1; x < 5; ++x) {
        [self drawInRect:CGRectMake(x, 0, self.size.width, self.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[x]];
        [self drawInRect:CGRectMake(-x, 0, self.size.width, self.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[x]];
    }
    UIImage *horizBlurredImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    // Blur vertically
    UIGraphicsBeginImageContext(self.size);
    [horizBlurredImage drawInRect:CGRectMake(0, 0, self.size.width, self.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[0]];
    for (int y = 1; y < 5; ++y) {
        [horizBlurredImage drawInRect:CGRectMake(0, y, self.size.width, self.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[y]];
        [horizBlurredImage drawInRect:CGRectMake(0, -y, self.size.width, self.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[y]];
    }
    UIImage *blurredImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    //
    return blurredImage;
}

And use it like this:

UIImage *blurredImage = [originalImage imageWithGaussianBlur];

To get stronger blur just apply this effect twice or more :)

Community
  • 1
  • 1
lukaswelte
  • 2,951
  • 1
  • 23
  • 45
  • Did you try it standalone? So just set a blurred image although if no button is pressed? Did you try all Options? – lukaswelte Apr 29 '13 at 11:55
1

You need to use a CoreImage Framework(Apple Doc), or any third party framework such as GPUImage(Github Page). Gaussian Blur is available in CoreImage(iOS) only starting from iOS 6. Of course you can use any other solutions, but the proper "filtering" is done with those frameworks.

Anatoliy Gatt
  • 2,501
  • 3
  • 26
  • 42