Skip to content

Instantly share code, notes, and snippets.

@brennanMKE
Last active December 3, 2020 11:40
Show Gist options
  • Save brennanMKE/10010625 to your computer and use it in GitHub Desktop.
Save brennanMKE/10010625 to your computer and use it in GitHub Desktop.
Screenshot of a view excluding views
- (UIImage *)screenshotOfView:(UIView *)view excludingViews:(NSArray *)excludedViews {
if (!floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
NSCAssert(FALSE, @"iOS 7 or later is required.");
}
// hide all excluded views before capturing screen and keep initial value
NSMutableArray *hiddenValues = [@[] mutableCopy];
for (NSUInteger index=0;index<excludedViews.count;index++) {
[hiddenValues addObject:[NSNumber numberWithBool:((UIView *)excludedViews[index]).hidden]];
((UIView *)excludedViews[index]).hidden = TRUE;
}
UIImage *image = nil;
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// reset hidden values
for (NSUInteger index=0;index<excludedViews.count;index++) {
((UIView *)excludedViews[index]).hidden = [[hiddenValues objectAtIndex:index] boolValue];
}
// clean up
hiddenValues = nil;
return image;
}
@brennanMKE
Copy link
Author

This method is the key to making a blurred menu where the contents behind the menu can be blurred and updated while the menu is still visible. It is used in my sample project for Blurred Menu to capture the contents of the view with the menu and blurred image hidden when the screenshot is captured.

It works by hiding the given views to exclude from the screenshot and setting them as hidden just before capturing the screenshot and then restoring their values immediately so the user does not see the change. Then the BlurredFrame Category derived from an WWDC presentation can be used to blur the image. It does not get the best performance, but it is functional enough for this purpose.

If a smaller area is blurred it will perform more quickly. In the Blurred Menu sample project it changes the text and background color of a view which appears under the menu and immediately updated the blurred background image behind the menu.

Another trick to make this effect work is to take advantage of clipping. The background view is actually stationary and attached to the right side of the view. The width is initially set to zero and grown to the width of the menu when it animates into view. The view is clipped so it only shows the section of the blurred image which is within the bounds of the clipped view. The menu then slides above it as the view which actually moves. This prevents the need to generate a blurred image for every key frame of the animation which would be far too slow.

@rakeshta
Copy link

There's an issue here. When you hide the views, capture image and then show the view again, the views being hidden will blink. Twice!
It's a side effect of setting the afterScreenUpdates: to YES.

I'm looking for a workaround for this issue.

@matt-curtis
Copy link

@rakeshta This happens because afterScreenUpdates makes the UI update immediately, instead of waiting until the end of the run loop. Looking for a solution to this now :/

@Kmohamed
Copy link

There's a weird issue on iPhone 7 simulator and Device it does not work with UIWindow.
If I exclude all subview of UIWindow the image will not be black but it works on iPhone 6 , 6s .....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment