@@ -40,27 +40,28 @@ - (ImageResult*) compressImageDimensions:(UIImage*)image
40
40
41
41
CGFloat oldWidth = image.size .width ;
42
42
CGFloat oldHeight = image.size .height ;
43
+
44
+ CGFloat widthScale = maxWidth / oldWidth;
45
+ CGFloat heightScale = maxHeight / oldHeight;
46
+ CGFloat scaleFactor = MIN (widthScale, heightScale);
47
+
48
+ CGFloat newWidth = oldWidth * scaleFactor;
49
+ CGFloat newHeight = oldHeight * scaleFactor;
43
50
44
- int newWidth = 0 ;
45
- int newHeight = 0 ;
46
-
47
- if (maxWidth < maxHeight) {
48
- newWidth = maxWidth;
49
- newHeight = (oldHeight / oldWidth) * newWidth;
50
- } else {
51
- newHeight = maxHeight;
52
- newWidth = (oldWidth / oldHeight) * newHeight;
53
- }
54
51
CGSize newSize = CGSizeMake (newWidth, newHeight);
55
-
56
- UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc ] initWithSize: newSize];
52
+
53
+ UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc ] init ];
54
+ format.scale = image.scale ;
55
+
56
+ UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc ] initWithSize: newSize format: format];
57
57
UIImage *resizedImage = [renderer imageWithActions: ^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
58
58
[image drawInRect: CGRectMake (0 , 0 , newSize.width, newSize.height)];
59
59
}];
60
60
61
- result.width = [ NSNumber numberWithFloat: newWidth] ;
62
- result.height = [ NSNumber numberWithFloat: newHeight] ;
61
+ result.width = @( newWidth) ;
62
+ result.height = @( newHeight) ;
63
63
result.image = resizedImage;
64
+
64
65
return result;
65
66
}
66
67
0 commit comments