Created
June 24, 2014 07:12
-
-
Save mamaz/ce1f607a5d816452bb1c to your computer and use it in GitHub Desktop.
ScaledToFillSize
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
// 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