Skip to content

Instantly share code, notes, and snippets.

@junpluse
Created April 25, 2013 12:51
Show Gist options
  • Save junpluse/5459459 to your computer and use it in GitHub Desktop.
Save junpluse/5459459 to your computer and use it in GitHub Desktop.
Instant UIView exporter
#import <UIKit/UIKit.h>
@interface UIView (Export)
- (UIImage *)exportAsImageWithScale:(CGFloat)scale;
@end
#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