Skip to content

Instantly share code, notes, and snippets.

@brennanMKE
Last active December 3, 2020 11:40
Show Gist options
  • Select an option

  • Save brennanMKE/10010625 to your computer and use it in GitHub Desktop.

Select an option

Save brennanMKE/10010625 to your computer and use it in GitHub Desktop.

Revisions

  1. brennanMKE revised this gist Apr 6, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion screenshotOfView.m
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ - (UIImage *)screenshotOfView:(UIView *)view excludingViews:(NSArray *)excludedV
    NSCAssert(FALSE, @"iOS 7 or later is required.");
    }

    // hide all exclude views before capturing screen and keep initial value
    // 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]];
  2. brennanMKE created this gist Apr 6, 2014.
    29 changes: 29 additions & 0 deletions screenshotOfView.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    - (UIImage *)screenshotOfView:(UIView *)view excludingViews:(NSArray *)excludedViews {
    if (!floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
    NSCAssert(FALSE, @"iOS 7 or later is required.");
    }

    // hide all exclude 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;
    }