三个观察点:
您需要将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示例代码(需要付费的开发者订阅),并下载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]
有趣的是,苹果的代码并没有使用 CIFilter
,而是调用了 vImageBoxConvolve_ARGB8888
,这是 vImage 高性能图像处理框架 的一部分。
这种技术在 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];
image
做什么。也许更新imageView.image = image;
? - Robradius
的CGFloat
变量,那么你可以这样写:[gaussianBlurFilter setValue:@(radius) forKey:kCIInputRadiusKey];
- Rob