縮小処理
こんな感じ。
@implementation UIImage (extension)
- (UIImage*)imageByShrinkingWithSize:(CGSize)size
{
CGFloat widthRatio = size.width / self.size.width;
CGFloat heightRatio = size.height / self.size.height;
CGFloat ratio = (widthRatio < heightRatio) ? widthRatio : heightRatio;
if (ratio >= 1.0) {
return self;
}
CGRect rect = CGRectMake(0, 0,
self.size.width * ratio,
self.size.height * ratio);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawInRect:rect];
UIImage* shrinkedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return shrinkedImage;
}
UIImage のカテゴリとして実装した。縦横比は保持したまま、与えられたサイズ内に収まる最大の画像を返す。参考情報
- 補間方法を指定してUIImageを縮小する(1) - tosh-tの日記
- 画像を縮小する時の補完方法について

