Created
January 15, 2015 10:16
-
-
Save Eridana/862d6f5e4675d0c17718 to your computer and use it in GitHub Desktop.
Crop UIImage
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 *)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