iOS 制作全屏截图问题

iOS make screenshot of entire screen issue

我有自拍和图片观看:

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];

    }];
}

正如您所看到的,我想根据主 window 视图模糊 "graphic context"。第一次当我展示自己的观点时它完美无缺,但后来,像模糊图像这样的东西相互叠加。

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

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

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

找到一些方法和图像类别 here

您创建了一个模糊循环。当你第二次截取视图时,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;
}