Skip to content

Instantly share code, notes, and snippets.

@asadm
Forked from brennanMKE/screenshotOfView.m
Created February 9, 2016 07:03
Show Gist options
  • Save asadm/2200be7f5a67a337a8fd to your computer and use it in GitHub Desktop.
Save asadm/2200be7f5a67a337a8fd 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment