Skip to content

Instantly share code, notes, and snippets.

@parachvte
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save parachvte/585a18a0064a638461e8 to your computer and use it in GitHub Desktop.

Select an option

Save parachvte/585a18a0064a638461e8 to your computer and use it in GitHub Desktop.
iOS - ImageCrop
@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