-
-
Save asadm/2200be7f5a67a337a8fd to your computer and use it in GitHub Desktop.
Screenshot of a view excluding views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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