作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
现在我正在构建一个需要模糊整个 UIView 的 iPhone 应用程序。我怎样才能做到这一点?我看过this framework ,但我认为这不适用于 UIView。有没有其他方法可以模糊 UIView?
更新:查看我更新后的答案,增加了与 iOS 7 和 iOS 8 的相关性。
最佳答案
这应该有效。我在代码中注释以帮助您了解发生了什么:
//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 在 iOS 5.1 中不存在,因此您必须找到一种不同的方法来模糊设备 5.x+ 的 View (感谢@BradLarson 提供此提示)。 this question 中接受的答案作为替代品看起来很有前途,this library 也是如此.
关于ios - 如何对 UIView 应用模糊?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11130923/
我是一名优秀的程序员,十分优秀!