Last active
December 10, 2015 12:58
-
-
Save codeswimmer/4437477 to your computer and use it in GitHub Desktop.
iOS: How To Crop Image
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 *)imageFromImage:(UIImage *)image inRect:(CGRect)rect { | |
| CGImageRef sourceImageRef = [image CGImage]; | |
| CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect); | |
| UIImage *newImage = [UIImage imageWithCGImage:newImageRef]; | |
| CGImageRelease(newImageRef); | |
| return newImage; | |
| } | |
| If you have implemented that function above. You can crop images using the code below (crop the image "whoeImage.png" to a rectangle (0,0,40,40)). | |
| UImage *img = [UIimage imageNamed:@"wholeImage.png"]; | |
| UIImage *croppedImage = [self imageFromImage:img inRect:CGRectMake(0, 0, 40, 40)]; | |
| Now you have a cropped UIImage named "croppedImage". | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment