0

我有图像视图的自我视图:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.bounds];
[self setBluredImageView:imageView];        
[self addSubview:imageView];

- (UIImage *)takeSnapshotOfView:(UIView *)view
{
    CGFloat reductionFactor = 1;
    UIGraphicsBeginImageContext(CGSizeMake(view.frame.size.width/reductionFactor, view.frame.size.height/reductionFactor));
    [view drawViewHierarchyInRect:CGRectMake(0, 0, view.frame.size.width/reductionFactor, view.frame.size.height/reductionFactor) afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

在方法中,当我想在其他视图上显示自己的视图时,我会这样做:

- (void)showMe; {


    AppDelegate* app = [AppDelegate shared];
    [app.window addSubview:self];

    UIImage *image = [self blurWithImageEffects:[self takeSnapshotOfView:app.window]];

    [[self bluredImageView] setImage:image];

    [UIView animateWithDuration:0.4 animations:^{

        [self setAlpha:1.0];

    }];
}

如您所见,我想模糊基于主窗口视图的“图形上下文”。当我第一次展示自己的视图时,它工作得很好,但是,像模糊图像这样的东西会相互叠加。

所以这是我第一次展示我的视图时的图像:

在此处输入图像描述

当我多次展示我的视图时,模糊的图像看起来像这样:

在此处输入图像描述

所以你可以看到每次模糊的截图都是不同的,但我使用相同的方法来获取截图,并且不更新视图控制器或其他 ui 部分的内容。

在这里找到一些方法和图像类别。

4

1 回答 1

1

您已经创建了一个模糊循环。当你第二次截取视图的截图时,bluredImageView 也在截图中。这就是为什么你看到效果成倍增加的原因。尝试删除它并仅捕获没有效果的上下文,然后将其添加回来

 - (UIImage *)takeSnapshotOfView:(UIView *)view
{
    //Remove the blured image before taking another screenshot.
    [self bluredImageView] removeFromSuperview];
    CGFloat reductionFactor = 1;

    UIGraphicsBeginImageContext(CGSizeMake(view.frame.size.width/reductionFactor, view.frame.size.height/reductionFactor));
    [view drawViewHierarchyInRect:CGRectMake(0, 0, view.frame.size.width/reductionFactor, view.frame.size.height/reductionFactor) afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //Add it back now that the effect is done
    [self addSubview:[self bluredImageView];
    return image;
}
于 2015-02-17T21:04:26.750 回答