如何将模糊应用于UIView?

vir*_*ndh 44 image-processing blur uiview ios

现在我正在构建一个需要模糊整个UIView的iPhone应用程序.我怎样才能做到这一点?我已经看到了这个框架,但我认为这不适用于UIView.是否有另一种模糊UIView的方法?

更新:检查下面我更新的答案,增加与iOS 7和iOS 8的更新相关性.

pas*_*aya 47

这应该工作.我在代码中评论过,以帮助您了解正在发生的事情:

//To take advantage of CIFilters, you have to import the Core Image framework
#import <CoreImage/CoreImage.h>

//Get a UIImage from the UIView
UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//Blur the UIImage with a CIFilter
CIImage *imageToBlur = [CIImage imageWithCGImage:viewImage.CGImage];    
CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"]; 
[gaussianBlurFilter setValue:imageToBlur forKey: @"inputImage"]; 
[gaussianBlurFilter setValue:[NSNumber numberWithFloat: 10] forKey: @"inputRadius"];
CIImage *resultImage = [gaussianBlurFilter valueForKey: @"outputImage"]; 
UIImage *endImage = [[UIImage alloc] initWithCIImage:resultImage];

//Place the UIImage in a UIImageView
UIImageView *newView = [[UIImageView alloc] initWithFrame:self.view.bounds];
newView.image = endImage;
[self.view addSubview:newView];
Run Code Online (Sandbox Code Playgroud)

如果您对代码有任何疑问,请将其留在评论中.

注意:从5.1开始,iOS上不存在CIGaussianBlur,因此您必须找到一种不同的方法来模糊设备5.x +的视图(感谢@BradLarson提供此技巧).这个问题中接受的答案看起来很有希望作为替代,这个库也是如此.

  • 请注意,从5.1开始,iOS上不存在CIGaussianBlur,因此您可能需要找到另一种在5.x及更早版本上完成此操作的方法. (10认同)

vir*_*ndh 28

自从iOS 7发布以来,这个问题已经得到了很多点击,我认为我应该跟进我最终做的事情.

我个人用photoshop模仿了这张照片,但是有许多很棒的第三方库可以做动态静态模糊,这里有一对:

我想发布这个,因为你不再需要截取各个帧或屏幕的截图.

iOS 8:随着即将发布的iOS 8版本,Apple已经内置了模糊的内容UIView,UIVisualEffectView(https://developer.apple.com/documentation/uikit/uivisualeffectview)


Rob*_*ert 11

在iOS 8中,您可以使用UIVisualEffectView模糊的视图.请参阅:https://developer.apple.com/documentation/uikit/uivisualeffectview