在iOS7中创建模糊效果 [英] Creating a blur effect in iOS7
问题描述
我几个小时以来一直在寻找这个问题的答案,而我无法理解。当我按下按钮按钮时,我想为图像添加高斯模糊效果。用户是添加图像的用户。
I have been searching for an answer to this question in a few hours now, and I just can't figure it out. I want to add a gaussian blur effect to the image when i press the button "Button". The user is the one that is adding the image.
我已经根据SO和网络上其他地方的来源为按钮创建了一个动作。不起作用。我究竟做错了什么?任何代码将不胜感激。这是我的按钮动作:
I have created an action for the "Button" based on sources from SO and other places on the web. It will not work. What am I doing wrong? Any code would be greatly appreciated. Here is my "Button" action:
- (IBAction)test:(id)sender {
CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
[gaussianBlurFilter setValue:imageView.image forKey: @"inputImage"];
[gaussianBlurFilter setValue:[NSNumber numberWithFloat: 10] forKey: @"inputRadius"];
}
如果您还需要我做的其他事情来回答这个问题,请让我知道:D
If you need anything else that I have done to answer the question, please let me know :D
推荐答案
三个观察结果:
-
您需要将
inputImage
设置为UIImage中的
:CIImage
You need to set the
inputImage
to be the theCIImage
from yourUIImage
:
[gaussianBlurFilter setValue:[CIImage imageWithCGImage:[imageView.image CGImage]] forKey:kCIInputImageKey];
我没有看到你抓住 outputImage
,例如:
CIImage *outputImage = [gaussianBlurFilter outputImage];
你可能想转换那个 CIImage
返回 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);
或者,如果你去 WWDC 2013示例代码(需要付费开发者订阅)并下载 iOS_UIImageEffects
,然后你可以获得 UIImage + ImageEffects
类别。这提供了一些新方法:
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];
有趣的是,Apple的代码不使用 CIFilter
,而是致电 vImageBoxConvolve_ARGB8888
Performance / Conceptual / vImage / Introduction / Introduction.html#// apple_ref / doc / uid / TP30001001> vImage高性能图像处理框架。
Interestingly, Apple's code does not employ CIFilter
, but rather calls vImageBoxConvolve_ARGB8888
of the vImage high-performance image processing framework.
WWDC 2013视频在iOS上实施Engaging UI 中说明了这种技术。
This technique is illustrated in WWDC 2013 video Implementing Engaging UI on iOS.
我知道问题是关于iOS 7,但是现在在iOS 8中可以为任何 UIView添加模糊效果
对象 UIBlurEffect
:
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];
这篇关于在iOS7中创建模糊效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!