Created
April 5, 2016 04:57
-
-
Save warpling/782352f98f2cb146090c2f381a9b15f2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// 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 |
This file contains hidden or 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
// | |
// 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