Creating a blur effect in iOS7

use*_*448 26 uiimage cifilter ciimage 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.

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"];
}
Run Code Online (Sandbox Code Playgroud)

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

这是我的用户界面

Rob*_*Rob 74

三点意见:

  1. 你需要设置inputImage成为了CIImageUIImage:

    [gaussianBlurFilter setValue:[CIImage imageWithCGImage:[imageView.image CGImage]] forKey:kCIInputImageKey];
    
    Run Code Online (Sandbox Code Playgroud)
  2. 我没有看到你抓住outputImage,例如:

    CIImage *outputImage = [gaussianBlurFilter outputImage];
    
    Run Code Online (Sandbox Code Playgroud)
  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]];  // 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);
Run Code Online (Sandbox Code Playgroud)

或者,如果您转到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;
Run Code Online (Sandbox Code Playgroud)

因此,为了模糊和图像并使其变亮(给出"磨砂玻璃"效果),您可以这样做:

UIImage *newImage = [image applyLightEffect];
Run Code Online (Sandbox Code Playgroud)

有趣的是,苹果公司的代码并没有采用CIFilter,而是要求vImageBoxConvolve_ARGB8888在中VIMAGE高性能的图像处理框架.

在iOS上的 WWDC 2013视频实施参与UI中说明了这种技术.


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

UIVisualEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];    
effectView.frame = imageView.bounds;
[imageView addSubview:effectView];
Run Code Online (Sandbox Code Playgroud)

  • @ user2891448假设你有一些名为`radius`的`CGFloat`变量,那么你要做`[gaussianBlurFilter setValue:@(radius)forKey:kCIInputRadiusKey];` (2认同)