我一直在寻找这个,但我找不到正确的答案。
我想制作一个按钮,如果你按下这个按钮,图像视图就会模糊不清。
我该怎么做?
答案 0 :(得分:8)
您可以使用StackBluriOS或GPUImage
或
试试这个(找到here):来自Stackoverflow
的回答@interface UIImage (ImageBlur)
- (UIImage *)imageWithGaussianBlur;
@end
@implementation UIImage (ImageBlur)
- (UIImage *)imageWithGaussianBlur {
float weight[5] = {0.2270270270, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162};
// Blur horizontally
UIGraphicsBeginImageContext(self.size);
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[0]];
for (int x = 1; x < 5; ++x) {
[self drawInRect:CGRectMake(x, 0, self.size.width, self.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[x]];
[self drawInRect:CGRectMake(-x, 0, self.size.width, self.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[x]];
}
UIImage *horizBlurredImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Blur vertically
UIGraphicsBeginImageContext(self.size);
[horizBlurredImage drawInRect:CGRectMake(0, 0, self.size.width, self.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[0]];
for (int y = 1; y < 5; ++y) {
[horizBlurredImage drawInRect:CGRectMake(0, y, self.size.width, self.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[y]];
[horizBlurredImage drawInRect:CGRectMake(0, -y, self.size.width, self.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[y]];
}
UIImage *blurredImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//
return blurredImage;
}
并像这样使用它:
UIImage *blurredImage = [originalImage imageWithGaussianBlur];
要获得更强的模糊,只需将此效果应用两次或更多次:)
答案 1 :(得分:1)
您需要使用CoreImage Framework(Apple Doc)或任何第三方框架,例如GPUImage(Github Page)。高斯模糊只能从iOS 6开始在CoreImage(iOS)中使用。当然,您可以使用任何其他解决方案,但适当的“过滤”是使用这些框架完成的。