Skip to content

Instantly share code, notes, and snippets.

@lamprosg
Created October 1, 2014 08:19
Show Gist options
  • Select an option

  • Save lamprosg/5d4caafeaa1ca6552d03 to your computer and use it in GitHub Desktop.

Select an option

Save lamprosg/5d4caafeaa1ca6552d03 to your computer and use it in GitHub Desktop.
(iOS) Capture a UIView as a UIImage
// capture the contents of the view in the rect specified
- (UIImage*)captureView:(UIView*)view rect:(CGRect)rect {
if (rect.size.width == 0 || rect.size.height == 0)
rect = view.bounds;
UIGraphicsBeginImageContext(view.bounds.size);
CGContextClipToRect(UIGraphicsGetCurrentContext(), rect);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *anImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef finalImgRef = CGImageCreateWithImageInRect(anImage.CGImage, rect);
UIImage *finalImg = [UIImage imageWithCGImage:finalImgRef];
CGImageRelease(finalImgRef);
return finalImg;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment