@@ -37,10 +37,10 @@ void SDAdjustSVGContentMode(SVGKImage * svgImage, UIViewContentMode contentMode,
37
37
case UIViewContentModeScaleAspectFit: {
38
38
CGFloat scale = smallestScaleUp < 1 .0f ? smallestScaleUp : biggestScaleDown;
39
39
CGSize targetSize = CGSizeApplyAffineTransform (imageSize, CGAffineTransformMakeScale (scale, scale));
40
- CGFloat x = ceil (viewSize.width - targetSize.width ) / 2 ;
41
- CGFloat y = ceil (viewSize.height - targetSize.height ) / 2 ;
40
+ xPosition = (viewSize.width - targetSize.width ) / 2 ;
41
+ yPosition = (viewSize.height - targetSize.height ) / 2 ;
42
42
svgImage.size = targetSize;
43
- svgImage.DOMTree .viewport = SVGRectMake (x, y , targetSize.width , targetSize.height );
43
+ svgImage.DOMTree .viewport = SVGRectMake (xPosition, yPosition , targetSize.width , targetSize.height );
44
44
// masksToBounds to clip the sublayer which beyond the viewport to match `UIImageView` behavior
45
45
svgImage.CALayerTree .masksToBounds = YES ;
46
46
}
@@ -55,71 +55,80 @@ void SDAdjustSVGContentMode(SVGKImage * svgImage, UIViewContentMode contentMode,
55
55
scale = hScale;
56
56
}
57
57
CGSize targetSize = CGSizeApplyAffineTransform (imageSize, CGAffineTransformMakeScale (scale, scale));
58
- svgImage.size = targetSize;
59
58
if (imageAspect < viewAspect) {
60
59
// need center y as well
61
- xPosition = targetSize. width / 2 ;
62
- yPosition = viewSize.height / 2 ;
60
+ xPosition = 0 ;
61
+ yPosition = (targetSize. height - viewSize.height ) / 2 ;
63
62
} else {
64
63
// need center x as well
65
- xPosition = viewSize.width / 2 ;
66
- yPosition = targetSize. height / 2 ;
64
+ xPosition = (targetSize. width - viewSize.width ) / 2 ;
65
+ yPosition = 0 ;
67
66
}
68
- svgImage.CALayerTree .position = CGPointMake (xPosition, yPosition);
67
+ svgImage.DOMTree .viewBox = SVGRectMake (xPosition, yPosition, imageSize.width , imageSize.height );
68
+ svgImage.size = targetSize;
69
69
}
70
70
break ;
71
71
case UIViewContentModeTop: {
72
- xPosition = viewSize.width / 2 ;
73
- yPosition = imageSize.height / 2 ;
74
- svgImage.CALayerTree .position = CGPointMake (xPosition, yPosition);
72
+ xPosition = (imageSize.width - viewSize.width ) / 2 ;
73
+ yPosition = 0 ;
74
+ svgImage.DOMTree .viewBox = SVGRectMake (xPosition, yPosition, imageSize.width , imageSize.height );
75
+ svgImage.size = imageSize;
75
76
}
76
77
break ;
77
78
case UIViewContentModeTopLeft: {
78
- xPosition = imageSize.width / 2 ;
79
- yPosition = imageSize.height / 2 ;
80
- svgImage.CALayerTree .position = CGPointMake (xPosition, yPosition);
79
+ xPosition = 0 ;
80
+ yPosition = 0 ;
81
+ svgImage.DOMTree .viewBox = SVGRectMake (xPosition, yPosition, imageSize.width , imageSize.height );
82
+ svgImage.size = imageSize;
81
83
}
82
84
break ;
83
85
case UIViewContentModeTopRight: {
84
- xPosition = -imageSize.width / 2 + viewSize.width ;
85
- yPosition = imageSize.height / 2 ;
86
- svgImage.CALayerTree .position = CGPointMake (xPosition, yPosition);
86
+ xPosition = imageSize.width - viewSize.width ;
87
+ yPosition = 0 ;
88
+ svgImage.DOMTree .viewBox = SVGRectMake (xPosition, yPosition, imageSize.width , imageSize.height );
89
+ svgImage.size = imageSize;
87
90
}
88
91
break ;
89
92
case UIViewContentModeCenter: {
90
- xPosition = viewSize.width / 2 ;
91
- yPosition = viewSize.height / 2 ;
92
- svgImage.CALayerTree .position = CGPointMake (xPosition, yPosition);
93
+ xPosition = (imageSize.width - viewSize.width ) / 2 ;
94
+ yPosition = (imageSize.height - viewSize.height ) / 2 ;
95
+ svgImage.DOMTree .viewBox = SVGRectMake (xPosition, yPosition, imageSize.width , imageSize.height );
96
+ svgImage.size = imageSize;
93
97
}
94
98
break ;
95
99
case UIViewContentModeLeft: {
96
- xPosition = imageSize.width / 2 ;
97
- yPosition = viewSize.height / 2 ;
98
- svgImage.CALayerTree .position = CGPointMake (xPosition, yPosition);
100
+ xPosition = 0 ;
101
+ yPosition = (imageSize.height - viewSize.height ) / 2 ;
102
+ svgImage.DOMTree .viewBox = SVGRectMake (xPosition, yPosition, imageSize.width , imageSize.height );
103
+ svgImage.size = imageSize;
99
104
}
100
105
break ;
101
106
case UIViewContentModeRight: {
102
- xPosition = -imageSize.width / 2 + viewSize.width ;
103
- yPosition = viewSize.height / 2 ;
104
- svgImage.CALayerTree .position = CGPointMake (xPosition, yPosition);
107
+ xPosition = imageSize.width - viewSize.width ;
108
+ yPosition = (imageSize.height - viewSize.height ) / 2 ;
109
+ svgImage.DOMTree .viewBox = SVGRectMake (xPosition, yPosition, imageSize.width , imageSize.height );
110
+ svgImage.size = imageSize;
105
111
}
106
112
break ;
107
113
case UIViewContentModeBottom: {
108
- xPosition = viewSize.width / 2 ;
109
- yPosition = -imageSize.height / 2 + viewSize.height ;
110
- svgImage.CALayerTree .position = CGPointMake (xPosition, yPosition);
114
+ xPosition = (imageSize.width - viewSize.width ) / 2 ;
115
+ yPosition = imageSize.height - viewSize.height ;
116
+ svgImage.DOMTree .viewBox = SVGRectMake (xPosition, yPosition, imageSize.width , imageSize.height );
117
+ svgImage.size = imageSize;
111
118
}
112
119
break ;
113
120
case UIViewContentModeBottomLeft: {
114
- xPosition = imageSize.width / 2 ;
115
- yPosition = -imageSize.height / 2 + viewSize.height ;
116
- svgImage.CALayerTree .position = CGPointMake (xPosition, yPosition);
121
+ xPosition = 0 ;
122
+ yPosition = imageSize.height - viewSize.height ;
123
+ svgImage.DOMTree .viewBox = SVGRectMake (xPosition, yPosition, imageSize.width , imageSize.height );
124
+ svgImage.size = imageSize;
117
125
}
118
126
break ;
119
127
case UIViewContentModeBottomRight: {
120
- xPosition = -imageSize.width / 2 + viewSize.width ;
121
- yPosition = -imageSize.height / 2 + viewSize.height ;
122
- svgImage.CALayerTree .position = CGPointMake (xPosition, yPosition);
128
+ xPosition = imageSize.width - viewSize.width ;
129
+ yPosition = imageSize.height - viewSize.height ;
130
+ svgImage.DOMTree .viewBox = SVGRectMake (xPosition, yPosition, imageSize.width , imageSize.height );
131
+ svgImage.size = imageSize;
123
132
}
124
133
break ;
125
134
case UIViewContentModeRedraw: {
0 commit comments