Search code examples
iosios7uiimageciimage

iOS make screenshot of entire screen issue


I have self view with image view:

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

In method when I want to show my self view over other view I make this:

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

    }];
}

So as you can see I want to blur "graphic context" that based on main window view. At first time when I present my self view it works perfect, but then, something like blurred image multiply each other.

So this is image when I just first show my view:

enter image description here

When I present my view few times the blurred image looks like this:

enter image description here

So as you can see each time blurred screenshot is different, but I use the same method for getting screenshot and don't update content of the view controller or other ui parts.

Some methods and image categories found here.


Solution

  • You've created a loop of blurring. When you take the screenshot of the view a second time, the bluredImageView is also in the screenshot. That is why you see the effect multiplied. Try removing it and only capturing the context without the effect, then adding it back

     - (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;
    }