Created
December 29, 2013 15:43
-
-
Save yaozhuoyu/8171607 to your computer and use it in GitHub Desktop.
UIColor生成UIimage CoreImage blur
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
@interface UIImage (Extension) | |
+ (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size; | |
+ (UIImage *)gaussianBlurImage:(UIImage *)image andInputRadius:(CGFloat)radius; | |
+ (UIImage *)gaussianBlurImageWithColor:(UIColor *)color andSize:(CGSize)size andInputRadius:(CGFloat)radius; | |
@end | |
@implementation UIImage (Extension) | |
+ (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size | |
{ | |
CGRect rect = CGRectMake(0, 0, size.width, size.height); | |
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGContextSetFillColorWithColor(context, color.CGColor); | |
CGContextFillRect(context, rect); | |
UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return img; | |
} | |
+ (UIImage *)gaussianBlurImage:(UIImage *)image andInputRadius:(CGFloat)radius | |
{ | |
if (!image) { | |
return nil; | |
} | |
CIContext *context = [CIContext contextWithOptions:nil]; | |
CIImage *inputImage = [CIImage imageWithCGImage:image.CGImage]; | |
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"]; | |
[filter setValue:inputImage forKey:kCIInputImageKey]; | |
[filter setValue:[NSNumber numberWithFloat:radius] forKey:@"inputRadius"]; | |
CIImage *result = [filter valueForKey:kCIOutputImageKey]; | |
CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]]; | |
return [UIImage imageWithCGImage:cgImage]; | |
} | |
+ (UIImage *)gaussianBlurImageWithColor:(UIColor *)color andSize:(CGSize)size andInputRadius:(CGFloat)radius | |
{ | |
UIImage *image = [UIImage imageWithColor:color andSize:size]; | |
if (image) { | |
return [UIImage gaussianBlurImage:image andInputRadius:radius]; | |
} | |
else { | |
return nil; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment