Created
July 4, 2013 15:20
-
-
Save mrtj/5928586 to your computer and use it in GitHub Desktop.
Create blurred image from any UIView #objective-c
This file contains hidden or 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
#import <UIKit/UIKit.h> | |
@interface UIView (Blur) | |
+(UIImage*)createBlurredImageFromView:(UIView*)view; | |
@end |
This file contains hidden or 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
#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