Created
December 3, 2013 12:34
-
-
Save sag333ar/7768478 to your computer and use it in GitHub Desktop.
Scale uiimage to specified resolution.
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 *)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