在 iOS7 中创建模糊效果 [英] Creating a blur effect in iOS7

查看:17
本文介绍了在 iOS7 中创建模糊效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个小时以来,我一直在寻找这个问题的答案,但我就是想不通.当我按下按钮按钮"时,我想为图像添加高斯模糊效果.用户是添加图像的人.

我根据来自 SO 和网络上其他地方的来源为按钮"创建了一个操作.不起作用.我究竟做错了什么?任何代码将不胜感激.这是我的按钮"操作:

- (IBAction)test:(id)sender {CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];[gaussianBlurFilter setValue:imageView.image forKey: @"inputImage"];[gaussianBlurFilter setValue:[NSNumber numberWithFloat: 10] forKey: @"inputRadius"];}

如果您需要我为回答问题所做的任何其他工作,请告诉我:D

解决方案

三个观察:

  1. 您需要将 inputImage 设置为 UIImage 中的 CIImage:

    [gaussianBlurFilter setValue:[CIImage imageWithCGImage:[imageView.image CGImage]] forKey:kCIInputImageKey];

  2. 我没有看到你抓取了 outputImage,例如:

    CIImage *outputImage = [gaussianBlurFilter outputImage];

  3. 并且您大概想将该 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]];//注意,如果你想要相同的大小,使用输入图像范围,输出图像范围更大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 *)tintColoraturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage *)maskImage;

因此,要模糊和成像并使其变亮(产生磨砂玻璃"效果),您可以执行以下操作:

UIImage *newImage = [image applyLightEffect];

有趣的是,Apple 的代码使用 CIFilter,而是调用 vImageBoxConvolve_ARGB8888vImage 高性能图像处理框架.

WWDC 2013 视频 在 iOS 上实现引人入胜的 UI 中说明了这种技术.><小时>

我知道问题是关于 iOS 7,但现在在 iOS 8 中,可以使用 UIBlurEffect 为任何 UIView 对象添加模糊效果:

UIVisualEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];effectView.frame = imageView.bounds;[imageView addSubview:effectView];

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.

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"];
}

If you need anything else that I have done to answer the question, please let me know :D

解决方案

Three observations:

  1. You need to set the inputImage to be the the CIImage from your UIImage:

    [gaussianBlurFilter setValue:[CIImage imageWithCGImage:[imageView.image CGImage]] forKey:kCIInputImageKey];
    

  2. I don't see you grabbing the outputImage, e.g.:

    CIImage *outputImage = [gaussianBlurFilter outputImage];
    

  3. 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];

这篇关于在 iOS7 中创建模糊效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆