-4

我一直在寻找这个,但我找不到正确的答案。

我想做一个按钮,如果你按下这个按钮,图像视图将被高斯模糊。

我怎样才能做到这一点?

4

2 回答 2

8

你可以使用StackBluriOSGPUImage

或者

试试这个(在这里找到):来自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];

要获得更强的模糊效果,只需应用此效果两次或更多次 :)

于 2013-04-29T11:42:51.347 回答
1

您需要使用 CoreImage 框架(Apple Doc)或任何第三方框架,例如 GPUImage(Github Page)。高斯模糊仅从 iOS 6 开始在 CoreImage(iOS) 中可用。当然您可以使用任何其他解决方案,但正确的“过滤”是通过这些框架完成的。

于 2013-04-29T11:44:25.650 回答