Skip to content

Instantly share code, notes, and snippets.

@bleft
Last active August 29, 2015 13:59
Show Gist options
  • Save bleft/10881428 to your computer and use it in GitHub Desktop.
Save bleft/10881428 to your computer and use it in GitHub Desktop.
UIImage from View
+ (UIImage *)imageFromView:(UIView *)view
{
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
if ([[UIScreen mainScreen] scale] == 2.0) {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 2.0);
} else {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 1.0);
}
} else {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 1.0);
}
BOOL hidden = [view isHidden];
[view setHidden:NO];
[[view layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[view setHidden:hidden];
return image;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment