Created
June 5, 2014 11:34
-
-
Save YGeorge/d8df24f29c349ec21a44 to your computer and use it in GitHub Desktop.
+ (UIImage *)scaleImage:(UIImage*)image toResolution:(int)resolution {
This file contains 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 *)scaleImage:(UIImage*)image toResolution:(int)resolution { | |
CGImageRef imgRef = [image CGImage]; | |
CGFloat width = CGImageGetWidth(imgRef); | |
CGFloat height = CGImageGetHeight(imgRef); | |
CGRect bounds = CGRectMake(0, 0, width, height); | |
//if already at the minimum resolution, return the orginal image, otherwise scale | |
if (width <= resolution && height <= resolution) { | |
return image; | |
} else { | |
CGFloat ratio = width/height; | |
if (ratio > 1) { | |
bounds.size.width = resolution; | |
bounds.size.height = bounds.size.width / ratio; | |
} else { | |
bounds.size.height = resolution; | |
bounds.size.width = bounds.size.height * ratio; | |
} | |
} | |
UIGraphicsBeginImageContext(bounds.size); | |
[image drawInRect:CGRectMake(0.0, 0.0, bounds.size.width, bounds.size.height)]; | |
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return imageCopy; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment