Skip to content

Commit 08f4524

Browse files
committed
Add support for preserve aspect ratio, this may be helpful for some user who want a scale to fill when generating bitmap representation.
1 parent 379c949 commit 08f4524

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ github "SVGKit/SVGKit" "2.x"
5353

5454
To use SVG coder, you should firstly add the `SDImageSVGCoder` to the coders manager. Then you can call the View Category method to start load SVG images.
5555

56-
Because SVG is a [vector image](https://en.wikipedia.org/wiki/Vector_graphics) format, which means it does not have a fixed bitmap size. However, `UIImage` or `CGImage` are all [bitmap image](https://en.wikipedia.org/wiki/Raster_graphics). For `UIImageView`, we will only parse SVG with a fixed image size (from the SVG viewPort information). But we also support you to specify a desired size during image loading using `SVGImageSize` context option.
56+
Because SVG is a [vector image](https://en.wikipedia.org/wiki/Vector_graphics) format, which means it does not have a fixed bitmap size. However, `UIImage` or `CGImage` are all [bitmap image](https://en.wikipedia.org/wiki/Raster_graphics). For `UIImageView`, we will only parse SVG with a fixed image size (from the SVG viewPort information). But we also support you to specify a desired size during image loading using `SVGImageSize` context option. And you can specify whether or not to keep aspect ratio during scale using `SVGImagePreserveAspectRatio` context option.
5757

5858
+ Objective-C
5959

SDWebImageSVGCoder/Classes/SDImageSVGCoder.m

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,27 @@ - (UIImage *)decodedImageWithData:(NSData *)data options:(SDImageCoderOptions *)
3838
return nil;
3939
}
4040

41-
// Check specified image size
41+
CGSize imageSize = CGSizeZero;
42+
BOOL preserveAspectRatio = YES;
43+
// Parse args
4244
SDWebImageContext *context = options[SDImageCoderWebImageContext];
4345
if (context[SDWebImageContextSVGImageSize]) {
4446
NSValue *sizeValue = context[SDWebImageContextSVGImageSize];
4547
#if SD_UIKIT
46-
CGSize imageSize = sizeValue.CGSizeValue;
48+
imageSize = sizeValue.CGSizeValue;
4749
#else
48-
CGSize imageSize = sizeValue.sizeValue;
50+
imageSize = sizeValue.sizeValue;
4951
#endif
50-
if (!CGSizeEqualToSize(imageSize, CGSizeZero)) {
51-
svgImage.size = imageSize;
52+
}
53+
if (context[SDWebImageContextSVGImagePreserveAspectRatio]) {
54+
preserveAspectRatio = [context[SDWebImageContextSVGImagePreserveAspectRatio] boolValue];
55+
}
56+
57+
if (!CGSizeEqualToSize(imageSize, CGSizeZero)) {
58+
if (preserveAspectRatio) {
59+
SDAdjustSVGContentMode(svgImage, UIViewContentModeScaleAspectFit, imageSize);
60+
} else {
61+
SDAdjustSVGContentMode(svgImage, UIViewContentModeScaleToFill, imageSize);
5262
}
5363
}
5464

SDWebImageSVGCoder/Classes/SDWebImageSVGCoderDefine.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,12 @@ FOUNDATION_EXPORT void SDAdjustSVGContentMode(SVGKImage * __nonnull svgImage, UI
2323

2424
/**
2525
A CGSize raw value which specify the desired SVG image size during image loading. Because vector image like SVG format, may not contains a fixed size, or you want to get a larger size bitmap representation UIImage. (NSValue)
26+
If you don't provide this value, use viewBox size of SVG for default value;
2627
*/
2728
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextSVGImageSize;
29+
30+
/**
31+
A BOOL value which specify the whether SVG image should keep aspect ratio during image loading. Because when you specify image size via `SDWebImageContextSVGImageSize`, we need to know whether to keep aspect ratio or not when image size aspect ratio is not equal to SVG viewBox size aspect ratio. (NSNumber)
32+
If you don't provide this value, use YES for default value.
33+
*/
34+
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextSVGImagePreserveAspectRatio;

SDWebImageSVGCoder/Classes/SDWebImageSVGCoderDefine.m

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void SDAdjustSVGContentMode(SVGKImage * svgImage, UIViewContentMode contentMode,
3636
}
3737
break;
3838
case UIViewContentModeScaleAspectFit: {
39-
CGFloat scale = smallestScaleUp < 1.0f ? smallestScaleUp : biggestScaleDown;
39+
CGFloat scale = smallestScaleUp < 1.0f ? biggestScaleDown : smallestScaleUp;
4040
CGSize targetSize = CGSizeApplyAffineTransform(imageSize, CGAffineTransformMakeScale(scale, scale));
4141
xPosition = (viewSize.width - targetSize.width) / 2;
4242
yPosition = (viewSize.height - targetSize.height) / 2;
@@ -65,71 +65,71 @@ void SDAdjustSVGContentMode(SVGKImage * svgImage, UIViewContentMode contentMode,
6565
xPosition = (targetSize.width - viewSize.width) / 2;
6666
yPosition = 0;
6767
}
68-
svgImage.DOMTree.viewBox = SVGRectMake(xPosition, yPosition, imageSize.width, imageSize.height);
6968
svgImage.size = targetSize;
69+
svgImage.DOMTree.viewBox = SVGRectMake(xPosition, yPosition, imageSize.width, imageSize.height);
7070
}
7171
break;
7272
case UIViewContentModeTop: {
7373
xPosition = (imageSize.width - viewSize.width) / 2;
7474
yPosition = 0;
75-
svgImage.DOMTree.viewBox = SVGRectMake(xPosition, yPosition, imageSize.width, imageSize.height);
7675
svgImage.size = imageSize;
76+
svgImage.DOMTree.viewBox = SVGRectMake(xPosition, yPosition, imageSize.width, imageSize.height);
7777
}
7878
break;
7979
case UIViewContentModeTopLeft: {
8080
xPosition = 0;
8181
yPosition = 0;
82-
svgImage.DOMTree.viewBox = SVGRectMake(xPosition, yPosition, imageSize.width, imageSize.height);
8382
svgImage.size = imageSize;
83+
svgImage.DOMTree.viewBox = SVGRectMake(xPosition, yPosition, imageSize.width, imageSize.height);
8484
}
8585
break;
8686
case UIViewContentModeTopRight: {
8787
xPosition = imageSize.width - viewSize.width;
8888
yPosition = 0;
89-
svgImage.DOMTree.viewBox = SVGRectMake(xPosition, yPosition, imageSize.width, imageSize.height);
9089
svgImage.size = imageSize;
90+
svgImage.DOMTree.viewBox = SVGRectMake(xPosition, yPosition, imageSize.width, imageSize.height);
9191
}
9292
break;
9393
case UIViewContentModeCenter: {
9494
xPosition = (imageSize.width - viewSize.width) / 2;
9595
yPosition = (imageSize.height - viewSize.height) / 2;
96-
svgImage.DOMTree.viewBox = SVGRectMake(xPosition, yPosition, imageSize.width, imageSize.height);
9796
svgImage.size = imageSize;
97+
svgImage.DOMTree.viewBox = SVGRectMake(xPosition, yPosition, imageSize.width, imageSize.height);
9898
}
9999
break;
100100
case UIViewContentModeLeft: {
101101
xPosition = 0;
102102
yPosition = (imageSize.height - viewSize.height) / 2;
103-
svgImage.DOMTree.viewBox = SVGRectMake(xPosition, yPosition, imageSize.width, imageSize.height);
104103
svgImage.size = imageSize;
104+
svgImage.DOMTree.viewBox = SVGRectMake(xPosition, yPosition, imageSize.width, imageSize.height);
105105
}
106106
break;
107107
case UIViewContentModeRight: {
108108
xPosition = imageSize.width - viewSize.width;
109109
yPosition = (imageSize.height - viewSize.height) / 2;
110-
svgImage.DOMTree.viewBox = SVGRectMake(xPosition, yPosition, imageSize.width, imageSize.height);
111110
svgImage.size = imageSize;
111+
svgImage.DOMTree.viewBox = SVGRectMake(xPosition, yPosition, imageSize.width, imageSize.height);
112112
}
113113
break;
114114
case UIViewContentModeBottom: {
115115
xPosition = (imageSize.width - viewSize.width) / 2;
116116
yPosition = imageSize.height - viewSize.height;
117-
svgImage.DOMTree.viewBox = SVGRectMake(xPosition, yPosition, imageSize.width, imageSize.height);
118117
svgImage.size = imageSize;
118+
svgImage.DOMTree.viewBox = SVGRectMake(xPosition, yPosition, imageSize.width, imageSize.height);
119119
}
120120
break;
121121
case UIViewContentModeBottomLeft: {
122122
xPosition = 0;
123123
yPosition = imageSize.height - viewSize.height;
124-
svgImage.DOMTree.viewBox = SVGRectMake(xPosition, yPosition, imageSize.width, imageSize.height);
125124
svgImage.size = imageSize;
125+
svgImage.DOMTree.viewBox = SVGRectMake(xPosition, yPosition, imageSize.width, imageSize.height);
126126
}
127127
break;
128128
case UIViewContentModeBottomRight: {
129129
xPosition = imageSize.width - viewSize.width;
130130
yPosition = imageSize.height - viewSize.height;
131-
svgImage.DOMTree.viewBox = SVGRectMake(xPosition, yPosition, imageSize.width, imageSize.height);
132131
svgImage.size = imageSize;
132+
svgImage.DOMTree.viewBox = SVGRectMake(xPosition, yPosition, imageSize.width, imageSize.height);
133133
}
134134
break;
135135
case UIViewContentModeRedraw: {
@@ -141,3 +141,4 @@ void SDAdjustSVGContentMode(SVGKImage * svgImage, UIViewContentMode contentMode,
141141
#endif
142142

143143
SDWebImageContextOption _Nonnull const SDWebImageContextSVGImageSize = @"svgImageSize";
144+
SDWebImageContextOption _Nonnull const SDWebImageContextSVGImagePreserveAspectRatio = @"svgImagePreserveAspectRatio";

0 commit comments

Comments
 (0)