Skip to content

Instantly share code, notes, and snippets.

@Eridana
Created January 15, 2015 10:16
Show Gist options
  • Save Eridana/862d6f5e4675d0c17718 to your computer and use it in GitHub Desktop.
Save Eridana/862d6f5e4675d0c17718 to your computer and use it in GitHub Desktop.
Crop UIImage
- (UIImage *)imageByCroppingImage:(UIImage *)image toSize:(CGSize)size
{
// not equivalent to image.size (which depends on the imageOrientation)!
double refWidth = CGImageGetWidth(image.CGImage);
double refHeight = CGImageGetHeight(image.CGImage);
double x = (refWidth - size.width) / 2.0;
double y = (refHeight - size.height) / 2.0;
CGRect cropRect = CGRectMake(x, y, size.height, size.width);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef scale:0.0 orientation:image.imageOrientation];
CGImageRelease(imageRef);
return cropped;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment