Created
October 1, 2014 08:19
-
-
Save lamprosg/5d4caafeaa1ca6552d03 to your computer and use it in GitHub Desktop.
(iOS) Capture a UIView as a UIImage
This file contains hidden or 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
| // 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