[英]Resize UIImage with the original ratio
我正在嘗試調整最大寬度為1000px的圖像的大小,但原始圖像分辨率的“比率”必須保持不變。 即1000x1300或1000x1600等
我需要對以下代碼進行哪些更改?
- (void)setImageAndConvertToThumb:(UIImage *)image {
//image
UIImage *sizedImg = [image scaleWithMaxSize:CGSizeMake(1000, 1000) quality:kCGInterpolationHigh];
NSData *data = UIImagePNGRepresentation(sizedImg);
self.image = data;
}
長寬比就是寬度除以高度(反之亦然),因此只需使用原始圖像具有的相同比率來計算新的高度,如下所示:
- (void)setImageAndConvertToThumb:(UIImage *)image {
//image
CGFloat newWidth = 1000;
CGFloat newHeight = newWidth * image.size.height / image.size.width;
UIImage *sizedImg = [image scaleWithMaxSize:CGSizeMake(newWidth, newHeight) quality:kCGInterpolationHigh];
NSData *data = UIImagePNGRepresentation(sizedImg);
self.image = data;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.