Skip to content

Instantly share code, notes, and snippets.

@hirokim
Created December 18, 2014 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hirokim/1ae69d9a99bba7de08a8 to your computer and use it in GitHub Desktop.
Save hirokim/1ae69d9a99bba7de08a8 to your computer and use it in GitHub Desktop.
アスペクト比を保ってUIImageをリサイズ
/**
* アスペクト比を保って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