Last active
August 29, 2015 14:23
-
-
Save parachvte/585a18a0064a638461e8 to your computer and use it in GitHub Desktop.
iOS - ImageCrop
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
| @implementation UIImage (ImageCrop) | |
| + (UIImage *)centerOvalCropImage:(UIImage *)image | |
| { | |
| CGFloat width = image.size.width; | |
| CGFloat height = image.size.height; | |
| CGRect targetRect = CGRectMake(width / 4.0, height / 4.0, width / 2.0, height / 2.0); | |
| UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, 0); | |
| CGContextRef context = UIGraphicsGetCurrentContext(); | |
| // Circle crop | |
| CGPathRef path = CGPathCreateWithEllipseInRect(targetRect, NULL); | |
| CGContextAddPath(context, path); | |
| CGContextClip(context); | |
| // Draw image in rect, it will be scaled automatically | |
| [image drawInRect:targetRect]; | |
| UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); | |
| UIGraphicsEndImageContext(); | |
| return newImage; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment