Skip to content

Instantly share code, notes, and snippets.

@mamaz
Created June 24, 2014 07:12
Show Gist options
  • Save mamaz/ce1f607a5d816452bb1c to your computer and use it in GitHub Desktop.
Save mamaz/ce1f607a5d816452bb1c to your computer and use it in GitHub Desktop.
ScaledToFillSize
// Return image Image scaledToFillSize, perfect for thumbnailing
// taken from http://stackoverflow.com/a/17884863/1328982
+ (UIImage *)imageWithImage:(UIImage *)image scaledToFillSize:(CGSize)size
{
CGFloat scale = MAX(size.width/image.size.width, size.height/image.size.height);
CGFloat width = image.size.width * scale;
CGFloat height = image.size.height * scale;
CGRect imageRect = CGRectMake((size.width - width)/2.0f,
(size.height - height)/2.0f,
width,
height);
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
[image drawInRect:imageRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment