Skip to content

Instantly share code, notes, and snippets.

@YGeorge
Created June 5, 2014 11:34
Show Gist options
  • Save YGeorge/d8df24f29c349ec21a44 to your computer and use it in GitHub Desktop.
Save YGeorge/d8df24f29c349ec21a44 to your computer and use it in GitHub Desktop.
+ (UIImage *)scaleImage:(UIImage*)image toResolution:(int)resolution {
+ (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