Three observations:
You need to set the inputImage
to be the the CIImage
from your UIImage
:
[gaussianBlurFilter setValue:[CIImage imageWithCGImage:[imageView.image CGImage]] forKey:kCIInputImageKey];
I don't see you grabbing the outputImage
, e.g.:
CIImage *outputImage = [gaussianBlurFilter outputImage];
And you presumably want to convert that CIImage
back to a UIImage
.
So, putting that all together:
CIFilter *gaussianBlurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
[gaussianBlurFilter setDefaults];
CIImage *inputImage = [CIImage imageWithCGImage:[imageView.image CGImage]];
[gaussianBlurFilter setValue:inputImage forKey:kCIInputImageKey];
[gaussianBlurFilter setValue:@10 forKey:kCIInputRadiusKey];
CIImage *outputImage = [gaussianBlurFilter outputImage];
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[inputImage extent]]; // note, use input image extent if you want it the same size, the output image extent is larger
UIImage *image = [UIImage imageWithCGImage:cgimg];
CGImageRelease(cgimg);
Alternatively, if you go to the WWDC 2013 sample code (paid developer subscription required) and download iOS_UIImageEffects
, you can then grab the UIImage+ImageEffects
category. That provides a few new methods:
- (UIImage *)applyLightEffect;
- (UIImage *)applyExtraLightEffect;
- (UIImage *)applyDarkEffect;
- (UIImage *)applyTintEffectWithColor:(UIColor *)tintColor;
- (UIImage *)applyBlurWithRadius:(CGFloat)blurRadius tintColor:(UIColor *)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage *)maskImage;
So, to blur and image and lightening it (giving that "frosted glass" effect) you can then do:
UIImage *newImage = [image applyLightEffect];
Interestingly, Apple's code does not employ CIFilter
, but rather calls vImageBoxConvolve_ARGB8888
of the vImage high-performance image processing framework.
This technique is illustrated in WWDC 2013 video Implementing Engaging UI on iOS.
I know the question was about iOS 7, but now in iOS 8 one can add a blur effect to any UIView
object with UIBlurEffect
:
UIVisualEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
effectView.frame = imageView.bounds;
[imageView addSubview:effectView];
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…