Created
December 18, 2014 08:59
-
-
Save hirokim/1ae69d9a99bba7de08a8 to your computer and use it in GitHub Desktop.
アスペクト比を保ってUIImageをリサイズ
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をリサイズ | |
* | |
*/ | |
+ (UIImage *)resizeAspectFitWithSize:(UIImage *)srcImg size:(CGSize)size | |
{ | |
CGFloat widthRatio = size.width / srcImg.size.width; | |
CGFloat heightRatio = size.height / srcImg.size.height; | |
CGFloat ratio = (widthRatio < heightRatio) ? widthRatio : heightRatio; | |
CGSize resizedSize = CGSizeMake(srcImg.size.width*ratio, srcImg.size.height*ratio); | |
UIGraphicsBeginImageContext(resizedSize); | |
[srcImg drawInRect:CGRectMake(0, 0, resizedSize.width, resizedSize.height)]; | |
UIImage* resizedImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return resizedImage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment