Skip to content

Instantly share code, notes, and snippets.

@warpling
Created April 5, 2016 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save warpling/782352f98f2cb146090c2f381a9b15f2 to your computer and use it in GitHub Desktop.
Save warpling/782352f98f2cb146090c2f381a9b15f2 to your computer and use it in GitHub Desktop.
//
// UIView+Screengrab.h
//
//
// Created by Ryan McLeod on 7/27/15.
//
//
#import <UIKit/UIKit.h>
@interface UIView (Screengrab)
- (UIImage*) captureImage;
- (UIImage*) captureImageInBounds:(CGRect)bounds;
- (UIImage*) captureImageAfterScreenUpdates:(BOOL)afterUpdates;
- (UIImage*) captureImageInBounds:(CGRect)bounds afterScreenUpdates:(BOOL)afterUpdates;
- (UIImage*) captureImageInBoundsAlt:(CGRect)bounds afterScreenUpdates:(BOOL)afterUpdates;
@end
//
// UIView+Screengrab.m
//
//
// Created by Ryan McLeod on 7/27/15.
//
//
#import "UIView+Screengrab.h"
@implementation UIView (Screengrab)
- (UIImage*) captureImage {
return [self captureImageAfterScreenUpdates:NO];
}
- (UIImage*) captureImageInBounds:(CGRect)bounds {
return [self captureImageInBounds:bounds afterScreenUpdates:NO];
}
// Get the current UIView as a UIImage
- (UIImage*) captureImageAfterScreenUpdates:(BOOL)afterUpdates {
return [self captureImageInBounds:self.bounds afterScreenUpdates:afterUpdates];
}
- (UIImage*) captureImageInBounds:(CGRect)bounds afterScreenUpdates:(BOOL)afterUpdates {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0.0f);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:afterUpdates];
UIImage * snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return snapshotImage;
}
- (UIImage*) captureImageInBoundsAlt:(CGRect)bounds afterScreenUpdates:(BOOL)afterUpdates {
UIImage * snapshotImage = [[self resizableSnapshotViewFromRect:bounds afterScreenUpdates:NO withCapInsets:UIEdgeInsetsZero] captureImageAfterScreenUpdates:YES];
UIGraphicsEndImageContext();
return snapshotImage;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment