Created
April 25, 2013 12:51
-
-
Save junpluse/5459459 to your computer and use it in GitHub Desktop.
Instant UIView exporter
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
#import <UIKit/UIKit.h> | |
@interface UIView (Export) | |
- (UIImage *)exportAsImageWithScale:(CGFloat)scale; | |
@end |
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
#import "UIView+Export.h" | |
#import <QuartzCore/QuartzCore.h> | |
@implementation UIView (Export) | |
- (UIImage *)exportAsImageWithScale:(CGFloat)scale | |
{ | |
CGRect bounds = [self exportBounds]; | |
UIGraphicsBeginImageContextWithOptions(bounds.size, NO, scale); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGContextSaveGState(context); | |
CGContextTranslateCTM(context, -bounds.origin.x, -bounds.origin.y); | |
[self.layer renderInContext:context]; | |
CGContextRestoreGState(context); | |
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return image; | |
} | |
- (CGRect)exportBounds | |
{ | |
CGRect bounds = [self convertRect:self.frame fromView:self.superview]; | |
for (UIView *subview in self.subviews) { | |
CGRect subviewBounds = [subview exportBounds]; | |
CGRect subviewFrame = [subview convertRect:subviewBounds toView:self]; | |
bounds = CGRectUnion(bounds, subviewFrame); | |
} | |
return bounds; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment