Skip to content

Instantly share code, notes, and snippets.

@mrtj
Created July 4, 2013 15:20
Show Gist options
  • Save mrtj/5928586 to your computer and use it in GitHub Desktop.
Save mrtj/5928586 to your computer and use it in GitHub Desktop.
Create blurred image from any UIView #objective-c
#import <UIKit/UIKit.h>
@interface UIView (Blur)
+(UIImage*)createBlurredImageFromView:(UIView*)view;
@end
#import "UIView+Blur.h"
#import <QuartzCore/QuartzCore.h>
@implementation UIView (Blur)
+(UIImage*)createBlurredImageFromView:(UIView*)view
{
//Get a UIImage from the UIView
CGSize size = view.bounds.size;
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Blur the UIImage
CIImage *imageToBlur = [CIImage imageWithCGImage:viewImage.CGImage];
CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
[gaussianBlurFilter setValue:imageToBlur forKey: @"inputImage"];
[gaussianBlurFilter setValue:[NSNumber numberWithFloat: 10] forKey: @"inputRadius"];
CIImage *bluredImage = [gaussianBlurFilter valueForKey: @"outputImage"];
CIFilter *cropFilter = [CIFilter filterWithName:@"CICrop"];
[cropFilter setValue:bluredImage forKey:@"inputImage"];
CIVector *cropRect = [CIVector vectorWithX:0.0 Y:0.0 Z:size.width W:size.height];
[cropFilter setValue:cropRect forKey:@"inputRectangle"];
CIImage *croppedImage = [cropFilter valueForKey:@"outputImage"];
UIImage *endImage = [[UIImage alloc] initWithCIImage:croppedImage];
return endImage;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment