@@ -24,8 +24,6 @@ void SDAdjustSVGContentMode(SVGKImage * svgImage, UIViewContentMode contentMode,
2424 CGFloat hScale = viewSize.height / imageSize.height ;
2525 CGFloat imageAspect = imageSize.width / imageSize.height ;
2626 CGFloat viewAspect = viewSize.width / viewSize.height ;
27- CGFloat smallestScaleUp = MIN (wScale, hScale);
28- CGFloat biggestScaleDown = MAX (wScale, hScale);
2927 CGFloat xPosition;
3028 CGFloat yPosition;
3129
@@ -36,10 +34,24 @@ void SDAdjustSVGContentMode(SVGKImage * svgImage, UIViewContentMode contentMode,
3634 }
3735 break ;
3836 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+ }
4045 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+ }
4355 svgImage.size = targetSize;
4456 svgImage.DOMTree .viewport = SVGRectMake (xPosition, yPosition, targetSize.width , targetSize.height );
4557 // masksToBounds to clip the sublayer which beyond the viewport to match `UIImageView` behavior
@@ -59,10 +71,10 @@ void SDAdjustSVGContentMode(SVGKImage * svgImage, UIViewContentMode contentMode,
5971 if (imageAspect < viewAspect) {
6072 // need center y as well
6173 xPosition = 0 ;
62- yPosition = (targetSize.height - viewSize.height ) / 2 ;
74+ yPosition = ABS (targetSize.height - viewSize.height ) / 2 ;
6375 } else {
6476 // need center x as well
65- xPosition = (targetSize.width - viewSize.width ) / 2 ;
77+ xPosition = ABS (targetSize.width - viewSize.width ) / 2 ;
6678 yPosition = 0 ;
6779 }
6880 svgImage.size = targetSize;
0 commit comments