我试图调整图像的大小,最大宽度为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;
}