三个观察:
-
您需要将inputImage 设置为您的UIImage 中的CIImage:
[gaussianBlurFilter setValue:[CIImage imageWithCGImage:[imageView.image CGImage]] forKey:kCIInputImageKey];
-
我没有看到你在抢outputImage,例如:
CIImage *outputImage = [gaussianBlurFilter outputImage];
您大概想将 CIImage 转换回 UIImage。
所以,把所有这些放在一起:
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);
或者,如果您转到WWDC 2013 sample code(需要付费开发者订阅)并下载iOS_UIImageEffects,则可以获取UIImage+ImageEffects 类别。这提供了一些新方法:
- (UIImage *)applyLightEffect;
- (UIImage *)applyExtraLightEffect;
- (UIImage *)applyDarkEffect;
- (UIImage *)applyTintEffectWithColor:(UIColor *)tintColor;
- (UIImage *)applyBlurWithRadius:(CGFloat)blurRadius tintColor:(UIColor *)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage *)maskImage;
因此,要模糊和成像并使其变亮(给出“毛玻璃”效果),您可以这样做:
UIImage *newImage = [image applyLightEffect];
有趣的是,Apple 的代码不使用CIFilter,而是调用vImage high-performance image processing framework 中的vImageBoxConvolve_ARGB8888。
这项技术在 WWDC 2013 视频Implementing Engaging UI on iOS 中有说明。
我知道问题是关于 iOS 7,但现在在 iOS 8 中,可以使用 UIBlurEffect 为任何 UIView 对象添加模糊效果:
UIVisualEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
effectView.frame = imageView.bounds;
[imageView addSubview:effectView];