Last active
December 10, 2015 07:38
-
-
Save psobko/4402753 to your computer and use it in GitHub Desktop.
Take a screenshot of a given view and optionally crop it.
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
+(UIImage*)TakeScreenshotOfView:(UIView*)view Cropping:(CGRect)dimensions | |
{ | |
CGFloat scale = 1.0f; | |
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) | |
scale = [[UIScreen mainScreen] scale]; | |
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, scale); | |
[view.layer renderInContext:UIGraphicsGetCurrentContext()]; | |
UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext(); | |
CGRect rect = CGRectMake(dimensions.origin.x , dimensions.origin.y * scale , dimensions.size.width * scale, dimensions.size.height * scale); | |
CGImageRef croppedImageRef = CGImageCreateWithImageInRect([fullImage CGImage], rect); | |
UIGraphicsEndImageContext(); | |
UIImage *croppedImage = [UIImage imageWithCGImage:croppedImageRef]; | |
return croppedImage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment