@@ -24,8 +24,6 @@ void SDAdjustSVGContentMode(SVGKImage * svgImage, UIViewContentMode contentMode,
24
24
CGFloat hScale = viewSize.height / imageSize.height ;
25
25
CGFloat imageAspect = imageSize.width / imageSize.height ;
26
26
CGFloat viewAspect = viewSize.width / viewSize.height ;
27
- CGFloat smallestScaleUp = MIN (wScale, hScale);
28
- CGFloat biggestScaleDown = MAX (wScale, hScale);
29
27
CGFloat xPosition;
30
28
CGFloat yPosition;
31
29
@@ -36,10 +34,24 @@ void SDAdjustSVGContentMode(SVGKImage * svgImage, UIViewContentMode contentMode,
36
34
}
37
35
break ;
38
36
case UIViewContentModeScaleAspectFit: {
39
- CGFloat scale = smallestScaleUp < 1 .0f ? biggestScaleDown : smallestScaleUp;
37
+ CGFloat scale;
38
+ if (imageAspect > viewAspect) {
39
+ // scale width
40
+ scale = wScale;
41
+ } else {
42
+ // scale height
43
+ scale = hScale;
44
+ }
40
45
CGSize targetSize = CGSizeApplyAffineTransform (imageSize, CGAffineTransformMakeScale (scale, scale));
41
- xPosition = (viewSize.width - targetSize.width ) / 2 ;
42
- yPosition = (viewSize.height - targetSize.height ) / 2 ;
46
+ if (imageAspect > viewAspect) {
47
+ // need center y as well
48
+ xPosition = 0 ;
49
+ yPosition = ABS (targetSize.height - viewSize.height ) / 2 ;
50
+ } else {
51
+ // need center x as well
52
+ xPosition = ABS (targetSize.width - viewSize.width ) / 2 ;
53
+ yPosition = 0 ;
54
+ }
43
55
svgImage.size = targetSize;
44
56
svgImage.DOMTree .viewport = SVGRectMake (xPosition, yPosition, targetSize.width , targetSize.height );
45
57
// masksToBounds to clip the sublayer which beyond the viewport to match `UIImageView` behavior
@@ -59,10 +71,10 @@ void SDAdjustSVGContentMode(SVGKImage * svgImage, UIViewContentMode contentMode,
59
71
if (imageAspect < viewAspect) {
60
72
// need center y as well
61
73
xPosition = 0 ;
62
- yPosition = (targetSize.height - viewSize.height ) / 2 ;
74
+ yPosition = ABS (targetSize.height - viewSize.height ) / 2 ;
63
75
} else {
64
76
// need center x as well
65
- xPosition = (targetSize.width - viewSize.width ) / 2 ;
77
+ xPosition = ABS (targetSize.width - viewSize.width ) / 2 ;
66
78
yPosition = 0 ;
67
79
}
68
80
svgImage.size = targetSize;
0 commit comments