Skip to content

Commit f59220e

Browse files
committed
Add the support to decode SVG into the bitmap representation, which is used for some image processing need bitmap
1 parent 8d0f2bf commit f59220e

File tree

8 files changed

+194
-40
lines changed

8 files changed

+194
-40
lines changed

Example/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
PODS:
22
- SDWebImage/Core (5.3.1)
3-
- SDWebImageSVGCoder (0.3.0):
3+
- SDWebImageSVGCoder (1.0.0):
44
- SDWebImage/Core (~> 5.0)
55

66
DEPENDENCIES:
@@ -16,7 +16,7 @@ EXTERNAL SOURCES:
1616

1717
SPEC CHECKSUMS:
1818
SDWebImage: 7137d57385fb632129838c1e6ab9528a22c666cc
19-
SDWebImageSVGCoder: 23a0505a7d34193abf93e9c1afda1dd5c12134d4
19+
SDWebImageSVGCoder: 664bfce6b8679147a9c15033b58941e124a47b9d
2020

2121
PODFILE CHECKSUM: 84f51f25230f3de246edc2d26479a6b409c99fb1
2222

Example/SDWebImageSVGCoder-Example-macOS/ViewController.m

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ - (void)viewDidLoad {
2020
[[SDImageCodersManager sharedManager] addCoder:SVGCoder];
2121
NSURL *svgURL = [NSURL URLWithString:@"https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/w3c.svg"];
2222
NSURL *svgURL2 = [NSURL URLWithString:@"https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/wikimedia.svg"];
23+
NSURL *svgURL3 = [NSURL URLWithString:@"https://simpleicons.org/icons/github.svg"];
2324

2425
CGSize screenSize = self.view.bounds.size;
2526

@@ -31,8 +32,13 @@ - (void)viewDidLoad {
3132
imageView2.frame = CGRectMake(screenSize.width / 2, 0, screenSize.width / 2, screenSize.height);
3233
imageView2.imageScaling = NSImageScaleProportionallyUpOrDown;
3334

35+
UIImageView *imageView3 = [[UIImageView alloc] init];
36+
imageView3.frame = CGRectMake(screenSize.width - 50, 0, 50, 50);
37+
imageView3.imageScaling = NSImageScaleAxesIndependently;
38+
3439
[self.view addSubview:imageView1];
3540
[self.view addSubview:imageView2];
41+
[self.view addSubview:imageView3];
3642

3743
[imageView1 sd_setImageWithURL:svgURL placeholderImage:nil options:SDWebImageRetryFailed completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
3844
if (image) {
@@ -45,18 +51,23 @@ - (void)viewDidLoad {
4551
if (image) {
4652
NSLog(@"SVG load animation success");
4753
[NSAnimationContext runAnimationGroup:^(NSAnimationContext * _Nonnull context) {
48-
NSAnimationContext *currentContext = [NSAnimationContext currentContext];
49-
currentContext.duration = 2;
54+
context.duration = 2;
5055
imageView2.animator.bounds = CGRectMake(0, 0, screenSize.width / 4, screenSize.height / 2);
5156
} completionHandler:^{
5257
[NSAnimationContext runAnimationGroup:^(NSAnimationContext * _Nonnull context) {
53-
NSAnimationContext *currentContext = [NSAnimationContext currentContext];
54-
currentContext.duration = 2;
58+
context.duration = 2;
5559
imageView2.animator.bounds = CGRectMake(0, 0, screenSize.width / 2, screenSize.height);
5660
} completionHandler:nil];
5761
}];
5862
}
5963
}];
64+
[imageView3 sd_setImageWithURL:svgURL3 placeholderImage:nil options:SDWebImageRetryFailed context:@{SDWebImageContextSVGPrefersBitmap: @(YES), SDWebImageContextSVGImageSize: @(CGSizeMake(50, 50))} progress:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
65+
if (image) {
66+
NSLog(@"SVG bitmap load success.");
67+
NSData *svgData = [image sd_imageDataAsFormat:SDImageFormatSVG];
68+
NSAssert(!svgData, @"SVG Data should not exist");
69+
}
70+
}];
6071
}
6172

6273

Example/SDWebImageSVGCoder/SDViewController.m

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,37 +36,40 @@ - (void)viewDidLoad
3636
imageView2.frame = CGRectMake(0, screenSize.height / 2, screenSize.width, screenSize.height / 2);
3737
imageView2.contentMode = UIViewContentModeScaleAspectFill;
3838

39-
UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(screenSize.width - 100, screenSize.height - 100, 100, 100)];
40-
imageView2.contentMode = UIViewContentModeScaleToFill;
39+
UIImageView *imageView3 = [[UIImageView alloc] init];
40+
imageView3.frame = CGRectMake(screenSize.width - 100, screenSize.height - 100, 100, 100);
41+
imageView3.contentMode = UIViewContentModeScaleToFill;
4142

4243
[self.view addSubview:imageView1];
4344
[self.view addSubview:imageView2];
4445
[self.view addSubview:imageView3];
4546

4647
[imageView1 sd_setImageWithURL:svgURL placeholderImage:nil options:SDWebImageRetryFailed completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
4748
if (image) {
48-
NSLog(@"SVGKLayeredImageView SVG load success");
49+
NSLog(@"SVG load success");
4950
NSData *svgData = [image sd_imageDataAsFormat:SDImageFormatSVG];
5051
NSAssert(svgData.length > 0, @"SVG Data should exist");
5152
}
5253
}];
5354
[imageView2 sd_setImageWithURL:svgURL2 placeholderImage:nil options:SDWebImageRetryFailed completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
54-
if (image) {
55-
NSLog(@"SVGKFastImageView SVG load success");
56-
}
57-
}];
58-
[imageView3 sd_setImageWithURL:svgURL3 placeholderImage:nil options:SDWebImageRetryFailed context:nil progress:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
5955
if (image) {
6056
NSLog(@"SVG load animation success");
6157
[UIView animateWithDuration:2 animations:^{
62-
imageView3.bounds = CGRectMake(0, 0, 300, 300);
58+
imageView2.bounds = CGRectMake(0, 0, screenSize.width * 2, screenSize.height);
6359
} completion:^(BOOL finished) {
6460
[UIView animateWithDuration:2 animations:^{
65-
imageView3.bounds = CGRectMake(0, 0, 100, 100);
61+
imageView2.bounds = CGRectMake(0, 0, screenSize.width, screenSize.height / 2);
6662
}];
6763
}];
6864
}
6965
}];
66+
[imageView3 sd_setImageWithURL:svgURL3 placeholderImage:nil options:SDWebImageRetryFailed context:@{SDWebImageContextSVGPrefersBitmap: @(YES), SDWebImageContextSVGImageSize: @(CGSizeMake(100, 100))} progress:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
67+
if (image) {
68+
NSLog(@"SVG bitmap load success.");
69+
NSData *svgData = [image sd_imageDataAsFormat:SDImageFormatSVG];
70+
NSAssert(!svgData, @"SVG Data should not exist");
71+
}
72+
}];
7073
}
7174

7275
- (void)didReceiveMemoryWarning

SDWebImageSVGCoder.xcodeproj/project.pbxproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
32407BF723799A8A00F9AFA4 /* SDImageSVGCoder.h in Headers */ = {isa = PBXBuildFile; fileRef = 32B6133C2170AB0F00DBD6ED /* SDImageSVGCoder.h */; settings = {ATTRIBUTES = (Public, ); }; };
1212
32407BF823799A8A00F9AFA4 /* SDImageSVGCoder.m in Sources */ = {isa = PBXBuildFile; fileRef = 32B613372170AB0F00DBD6ED /* SDImageSVGCoder.m */; };
1313
32407BFB23799A9200F9AFA4 /* SDWebImageSVGCoder.h in Headers */ = {isa = PBXBuildFile; fileRef = 32B613492170AB1600DBD6ED /* SDWebImageSVGCoder.h */; settings = {ATTRIBUTES = (Public, ); }; };
14+
32870719237D0497006DB55D /* SDWebImageSVGCoderDefine.h in Headers */ = {isa = PBXBuildFile; fileRef = 32870717237D0497006DB55D /* SDWebImageSVGCoderDefine.h */; settings = {ATTRIBUTES = (Public, ); }; };
15+
3287071A237D0497006DB55D /* SDWebImageSVGCoderDefine.h in Headers */ = {isa = PBXBuildFile; fileRef = 32870717237D0497006DB55D /* SDWebImageSVGCoderDefine.h */; settings = {ATTRIBUTES = (Public, ); }; };
16+
3287071B237D0497006DB55D /* SDWebImageSVGCoderDefine.h in Headers */ = {isa = PBXBuildFile; fileRef = 32870717237D0497006DB55D /* SDWebImageSVGCoderDefine.h */; settings = {ATTRIBUTES = (Public, ); }; };
17+
3287071C237D0497006DB55D /* SDWebImageSVGCoderDefine.h in Headers */ = {isa = PBXBuildFile; fileRef = 32870717237D0497006DB55D /* SDWebImageSVGCoderDefine.h */; settings = {ATTRIBUTES = (Public, ); }; };
18+
3287071D237D0497006DB55D /* SDWebImageSVGCoderDefine.m in Sources */ = {isa = PBXBuildFile; fileRef = 32870718237D0497006DB55D /* SDWebImageSVGCoderDefine.m */; };
19+
3287071E237D0497006DB55D /* SDWebImageSVGCoderDefine.m in Sources */ = {isa = PBXBuildFile; fileRef = 32870718237D0497006DB55D /* SDWebImageSVGCoderDefine.m */; };
20+
3287071F237D0497006DB55D /* SDWebImageSVGCoderDefine.m in Sources */ = {isa = PBXBuildFile; fileRef = 32870718237D0497006DB55D /* SDWebImageSVGCoderDefine.m */; };
21+
32870720237D0497006DB55D /* SDWebImageSVGCoderDefine.m in Sources */ = {isa = PBXBuildFile; fileRef = 32870718237D0497006DB55D /* SDWebImageSVGCoderDefine.m */; };
1422
328C14BE2184CEE7006B0C4A /* SDImageSVGCoder.h in Headers */ = {isa = PBXBuildFile; fileRef = 32B6133C2170AB0F00DBD6ED /* SDImageSVGCoder.h */; settings = {ATTRIBUTES = (Public, ); }; };
1523
328C14BF2184CEE7006B0C4A /* SDImageSVGCoder.m in Sources */ = {isa = PBXBuildFile; fileRef = 32B613372170AB0F00DBD6ED /* SDImageSVGCoder.m */; };
1624
328C14C62184CEE8006B0C4A /* SDImageSVGCoder.h in Headers */ = {isa = PBXBuildFile; fileRef = 32B6133C2170AB0F00DBD6ED /* SDImageSVGCoder.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -28,6 +36,8 @@
2836
/* Begin PBXFileReference section */
2937
32407BEF23799A5900F9AFA4 /* SDWebImageSVGCoder.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SDWebImageSVGCoder.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3038
32407BFC23799AC400F9AFA4 /* SDWebImage.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDWebImage.framework; path = Carthage/Build/watchOS/SDWebImage.framework; sourceTree = "<group>"; };
39+
32870717237D0497006DB55D /* SDWebImageSVGCoderDefine.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SDWebImageSVGCoderDefine.h; sourceTree = "<group>"; };
40+
32870718237D0497006DB55D /* SDWebImageSVGCoderDefine.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDWebImageSVGCoderDefine.m; sourceTree = "<group>"; };
3141
328C14B62184C856006B0C4A /* SDWebImageSVGCoder.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SDWebImageSVGCoder.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3242
328C14D02184D111006B0C4A /* SDWebImage.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDWebImage.framework; path = Carthage/Build/tvOS/SDWebImage.framework; sourceTree = "<group>"; };
3343
328C14D22184D11F006B0C4A /* SVGKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SVGKit.framework; path = Carthage/Build/tvOS/SVGKit.framework; sourceTree = "<group>"; };
@@ -114,6 +124,8 @@
114124
children = (
115125
32B6133C2170AB0F00DBD6ED /* SDImageSVGCoder.h */,
116126
32B613372170AB0F00DBD6ED /* SDImageSVGCoder.m */,
127+
32870717237D0497006DB55D /* SDWebImageSVGCoderDefine.h */,
128+
32870718237D0497006DB55D /* SDWebImageSVGCoderDefine.m */,
117129
);
118130
path = Classes;
119131
sourceTree = "<group>";
@@ -141,6 +153,7 @@
141153
files = (
142154
32407BFB23799A9200F9AFA4 /* SDWebImageSVGCoder.h in Headers */,
143155
32407BF723799A8A00F9AFA4 /* SDImageSVGCoder.h in Headers */,
156+
3287071C237D0497006DB55D /* SDWebImageSVGCoderDefine.h in Headers */,
144157
);
145158
runOnlyForDeploymentPostprocessing = 0;
146159
};
@@ -150,6 +163,7 @@
150163
files = (
151164
328C14C62184CEE8006B0C4A /* SDImageSVGCoder.h in Headers */,
152165
328C14CE2184CEF3006B0C4A /* SDWebImageSVGCoder.h in Headers */,
166+
3287071B237D0497006DB55D /* SDWebImageSVGCoderDefine.h in Headers */,
153167
);
154168
runOnlyForDeploymentPostprocessing = 0;
155169
};
@@ -159,6 +173,7 @@
159173
files = (
160174
32B613452170AB0F00DBD6ED /* SDImageSVGCoder.h in Headers */,
161175
32B6134A2170AB1600DBD6ED /* SDWebImageSVGCoder.h in Headers */,
176+
32870719237D0497006DB55D /* SDWebImageSVGCoderDefine.h in Headers */,
162177
);
163178
runOnlyForDeploymentPostprocessing = 0;
164179
};
@@ -168,6 +183,7 @@
168183
files = (
169184
328C14BE2184CEE7006B0C4A /* SDImageSVGCoder.h in Headers */,
170185
328C14CF2184CEF3006B0C4A /* SDWebImageSVGCoder.h in Headers */,
186+
3287071A237D0497006DB55D /* SDWebImageSVGCoderDefine.h in Headers */,
171187
);
172188
runOnlyForDeploymentPostprocessing = 0;
173189
};
@@ -326,6 +342,7 @@
326342
buildActionMask = 2147483647;
327343
files = (
328344
32407BF823799A8A00F9AFA4 /* SDImageSVGCoder.m in Sources */,
345+
32870720237D0497006DB55D /* SDWebImageSVGCoderDefine.m in Sources */,
329346
);
330347
runOnlyForDeploymentPostprocessing = 0;
331348
};
@@ -334,6 +351,7 @@
334351
buildActionMask = 2147483647;
335352
files = (
336353
328C14C72184CEE8006B0C4A /* SDImageSVGCoder.m in Sources */,
354+
3287071F237D0497006DB55D /* SDWebImageSVGCoderDefine.m in Sources */,
337355
);
338356
runOnlyForDeploymentPostprocessing = 0;
339357
};
@@ -342,6 +360,7 @@
342360
buildActionMask = 2147483647;
343361
files = (
344362
32B613402170AB0F00DBD6ED /* SDImageSVGCoder.m in Sources */,
363+
3287071D237D0497006DB55D /* SDWebImageSVGCoderDefine.m in Sources */,
345364
);
346365
runOnlyForDeploymentPostprocessing = 0;
347366
};
@@ -350,6 +369,7 @@
350369
buildActionMask = 2147483647;
351370
files = (
352371
328C14BF2184CEE7006B0C4A /* SDImageSVGCoder.m in Sources */,
372+
3287071E237D0497006DB55D /* SDWebImageSVGCoderDefine.m in Sources */,
353373
);
354374
runOnlyForDeploymentPostprocessing = 0;
355375
};

0 commit comments

Comments
 (0)