Create a gist now

Instantly share code, notes, and snippets.

Create a UIImage from a UIView
@interface UIView (UIImageCreation)
- (UIImage *)createImageFromRect:(CGRect)frame;
- (UIImage *)createImage;
@implementation UIView (UIImageCreation)
- (UIImage *)createImageFromRect:(CGRect)frame
CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint origin = [self bounds].origin;
CGPoint offset = frame.origin;
CGContextTranslateCTM(context, origin.x - offset.x, origin.y - offset.y);
[[self layer] renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
return image;
- (UIImage *)createImage
return [self createImageFromRect:[self bounds]];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment