Created
November 4, 2012 01:56
-
-
Save kevinkirkup/4009790 to your computer and use it in GitHub Desktop.
UIImage blur filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://stackoverflow.com/questions/8528726/does-ios-5-support-blur-coreimage-fiters | |
@interface UIImage (ImageBlur) | |
- (UIImage *)imageWithGaussianBlur9; | |
@end | |
@implementation UIImage (ImageBlur) | |
- (UIImage *)imageWithGaussianBlur9 { | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment