这应该工作。我在代码中添加了注释,以帮助您了解发生了什么:
//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];
如果您对代码有任何疑问,请将其保留在注释中。
注意: CIGaussianBlur从5.1版本开始在iOS上不存在,因此您必须找到另一种方法来模糊设备5.x +的视图(感谢@BradLarson)。在这个问题上被接受的答案看起来很有希望替代,就像这个图书馆一样 。
0
现在,我正在构建一个iPhone应用程序,该应用程序需要模糊整个UIView。我该如何实现?我已经看到了这个框架 ,但是我认为不适用于UIView。有没有一种模糊UIView的替代方法?
更新 :在下面检查我的更新答案,以了解更多有关iOS 7和iOS 8的问世。