iOS制作整个屏幕问题的截图

时间:2015-02-17 19:49:27

标签: ios ios7 uiimage ciimage

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

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

    }];
}

所以你可以看到我想模糊"图形背景"基于主窗口视图。在我第一次展示我的自我视图时,它的工作非常完美,但是,像模糊图像这样的东西会相互增加。

所以这是我第一次看到我的观点时的形象:

enter image description here

当我几次呈现我的视图时,模糊的图像看起来像这样:

enter image description here

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

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

1 个答案:

答案 0 :(得分: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;
}