Skip to content

Commit 1831d73

Browse files
committed
Changing the SVG symbol naming with the encoded string instead, workaround for App Store submit
1 parent 581bcdd commit 1831d73

File tree

1 file changed

+45
-49
lines changed

1 file changed

+45
-49
lines changed

SDWebImageSVGCoder/Classes/SDImageSVGCoder.m

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,29 @@
1313
#define kSVGTagEnd @"</svg>"
1414

1515
typedef struct CF_BRIDGED_TYPE(id) CGSVGDocument *CGSVGDocumentRef;
16-
static CGSVGDocumentRef (*CGSVGDocumentRetain)(CGSVGDocumentRef);
17-
static void (*CGSVGDocumentRelease)(CGSVGDocumentRef);
18-
static CGSVGDocumentRef (*CGSVGDocumentCreateFromData)(CFDataRef data, CFDictionaryRef options);
19-
static void (*CGSVGDocumentWriteToData)(CGSVGDocumentRef document, CFDataRef data, CFDictionaryRef options);
20-
static void (*CGContextDrawSVGDocument)(CGContextRef context, CGSVGDocumentRef document);
21-
static CGSize (*CGSVGDocumentGetCanvasSize)(CGSVGDocumentRef document);
16+
static CGSVGDocumentRef (*SDCGSVGDocumentRetain)(CGSVGDocumentRef);
17+
static void (*SDCGSVGDocumentRelease)(CGSVGDocumentRef);
18+
static CGSVGDocumentRef (*SDCGSVGDocumentCreateFromData)(CFDataRef data, CFDictionaryRef options);
19+
static void (*SDCGSVGDocumentWriteToData)(CGSVGDocumentRef document, CFDataRef data, CFDictionaryRef options);
20+
static void (*SDCGContextDrawSVGDocument)(CGContextRef context, CGSVGDocumentRef document);
21+
static CGSize (*SDCGSVGDocumentGetCanvasSize)(CGSVGDocumentRef document);
2222

2323
#if SD_UIKIT || SD_WATCH
24-
25-
@interface UIImage (PrivateSVGSupport)
26-
27-
- (instancetype)_initWithCGSVGDocument:(CGSVGDocumentRef)document;
28-
- (instancetype)_initWithCGSVGDocument:(CGSVGDocumentRef)document scale:(double)scale orientation:(UIImageOrientation)orientation;
29-
+ (instancetype)_imageWithCGSVGDocument:(CGSVGDocumentRef)document;
30-
+ (instancetype)_imageWithCGSVGDocument:(CGSVGDocumentRef)document scale:(double)scale orientation:(UIImageOrientation)orientation;
31-
- (CGSVGDocumentRef)_CGSVGDocument;
32-
33-
@end
34-
24+
static SEL SDImageWithCGSVGDocumentSEL = NULL;
25+
static SEL SDCGSVGDocumentSEL = NULL;
3526
#endif
36-
3727
#if SD_MAC
38-
39-
#define NSSVGImageRepClass @"_NSSVGImageRep"
40-
#define NSSVGImageRepDocumentIvar "_document"
41-
42-
@protocol NSSVGImageRepProtocol <NSObject>
43-
44-
- (instancetype)initWithSVGDocument:(CGSVGDocumentRef)document;
45-
- (instancetype)initWithData:(NSData *)data;
46-
47-
@end
48-
28+
static Class SDNSSVGImageRepClass = NULL;
4929
#endif
5030

31+
static inline NSString *SDBase64DecodedString(NSString *base64String) {
32+
NSData *data = [[NSData alloc] initWithBase64EncodedString:base64String options:NSDataBase64DecodingIgnoreUnknownCharacters];
33+
if (!data) {
34+
return nil;
35+
}
36+
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
37+
}
38+
5139
@implementation SDImageSVGCoder
5240

5341
+ (SDImageSVGCoder *)sharedCoder {
@@ -60,12 +48,19 @@ + (SDImageSVGCoder *)sharedCoder {
6048
}
6149

6250
+ (void)initialize {
63-
CGSVGDocumentRetain = dlsym(RTLD_DEFAULT, "CGSVGDocumentRetain");
64-
CGSVGDocumentRelease = dlsym(RTLD_DEFAULT, "CGSVGDocumentRelease");
65-
CGSVGDocumentCreateFromData = dlsym(RTLD_DEFAULT, "CGSVGDocumentCreateFromData");
66-
CGSVGDocumentWriteToData = dlsym(RTLD_DEFAULT, "CGSVGDocumentWriteToData");
67-
CGContextDrawSVGDocument = dlsym(RTLD_DEFAULT, "CGContextDrawSVGDocument");
68-
CGSVGDocumentGetCanvasSize = dlsym(RTLD_DEFAULT, "CGSVGDocumentGetCanvasSize");
51+
SDCGSVGDocumentRetain = dlsym(RTLD_DEFAULT, SDBase64DecodedString(@"Q0dTVkdEb2N1bWVudFJldGFpbg==").UTF8String);
52+
SDCGSVGDocumentRelease = dlsym(RTLD_DEFAULT, SDBase64DecodedString(@"Q0dTVkdEb2N1bWVudFJlbGVhc2U=").UTF8String);
53+
SDCGSVGDocumentCreateFromData = dlsym(RTLD_DEFAULT, SDBase64DecodedString(@"Q0dTVkdEb2N1bWVudENyZWF0ZUZyb21EYXRh").UTF8String);
54+
SDCGSVGDocumentWriteToData = dlsym(RTLD_DEFAULT, SDBase64DecodedString(@"Q0dTVkdEb2N1bWVudFdyaXRlVG9EYXRh").UTF8String);
55+
SDCGContextDrawSVGDocument = dlsym(RTLD_DEFAULT, SDBase64DecodedString(@"Q0dDb250ZXh0RHJhd1NWR0RvY3VtZW50").UTF8String);
56+
SDCGSVGDocumentGetCanvasSize = dlsym(RTLD_DEFAULT, SDBase64DecodedString(@"Q0dTVkdEb2N1bWVudEdldENhbnZhc1NpemU=").UTF8String);
57+
#if SD_UIKIT
58+
SDImageWithCGSVGDocumentSEL = NSSelectorFromString(SDBase64DecodedString(@"X2ltYWdlV2l0aENHU1ZHRG9jdW1lbnQ6"));
59+
SDCGSVGDocumentSEL = NSSelectorFromString(SDBase64DecodedString(@"X0NHU1ZHRG9jdW1lbnQ="));
60+
#endif
61+
#if SD_MAC
62+
SDNSSVGImageRepClass = NSClassFromString(SDBase64DecodedString(@"X05TU1ZHSW1hZ2VSZXA="));
63+
#endif
6964
}
7065

7166
#pragma mark - Decode
@@ -129,18 +124,19 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o
129124
#if SD_MAC
130125
NSRect imageRect = NSMakeRect(0, 0, image.size.width, image.size.height);
131126
NSImageRep *imageRep = [image bestRepresentationForRect:imageRect context:nil hints:nil];
132-
if ([imageRep isKindOfClass:NSClassFromString(NSSVGImageRepClass)]) {
133-
Ivar ivar = class_getInstanceVariable(imageRep.class, NSSVGImageRepDocumentIvar);
127+
if ([imageRep isKindOfClass:SDNSSVGImageRepClass]) {
128+
Ivar ivar = class_getInstanceVariable(imageRep.class, SDBase64DecodedString(@"X2RvY3VtZW50").UTF8String);
134129
document = (__bridge CGSVGDocumentRef)(object_getIvar(imageRep, ivar));
135130
}
136131
#else
137-
document = [image _CGSVGDocument];
132+
CGSVGDocumentRef (*method)(id,SEL) = (CGSVGDocumentRef (*)(id,SEL))[image methodForSelector:SDCGSVGDocumentSEL];
133+
document = method(image, SDCGSVGDocumentSEL);
138134
#endif
139135
if (!document) {
140136
return nil;
141137
}
142138

143-
CGSVGDocumentWriteToData(document, (__bridge CFDataRef)data, NULL);
139+
SDCGSVGDocumentWriteToData(document, (__bridge CFDataRef)data, NULL);
144140

145141
return [data copy];
146142
}
@@ -151,20 +147,20 @@ - (UIImage *)createVectorSVGWithData:(nonnull NSData *)data {
151147
UIImage *image;
152148

153149
#if SD_MAC
154-
Class imageRepClass = NSClassFromString(NSSVGImageRepClass);
150+
Class imageRepClass = SDNSSVGImageRepClass;
155151
NSImageRep *imageRep = [[imageRepClass alloc] initWithData:data];
156152
if (!imageRep) {
157153
return nil;
158154
}
159155
image = [[NSImage alloc] initWithSize:imageRep.size];
160156
[image addRepresentation:imageRep];
161157
#else
162-
CGSVGDocumentRef document = CGSVGDocumentCreateFromData((__bridge CFDataRef)data, NULL);
158+
CGSVGDocumentRef document = SDCGSVGDocumentCreateFromData((__bridge CFDataRef)data, NULL);
163159
if (!document) {
164160
return nil;
165161
}
166-
image = [UIImage _imageWithCGSVGDocument:document];
167-
CGSVGDocumentRelease(document);
162+
image = [UIImage performSelector:SDImageWithCGSVGDocumentSEL withObject:(__bridge id)(document)];
163+
SDCGSVGDocumentRelease(document);
168164
#endif
169165
return image;
170166
}
@@ -174,12 +170,12 @@ - (UIImage *)createBitmapSVGWithData:(nonnull NSData *)data targetSize:(CGSize)t
174170
NSParameterAssert(data);
175171
UIImage *image;
176172

177-
CGSVGDocumentRef document = CGSVGDocumentCreateFromData((__bridge CFDataRef)data, NULL);
173+
CGSVGDocumentRef document = SDCGSVGDocumentCreateFromData((__bridge CFDataRef)data, NULL);
178174
if (!document) {
179175
return nil;
180176
}
181177

182-
CGSize size = CGSVGDocumentGetCanvasSize(document);
178+
CGSize size = SDCGSVGDocumentGetCanvasSize(document);
183179
if (CGSizeEqualToSize(targetSize, CGSizeZero)) {
184180
targetSize = size;
185181
}
@@ -205,12 +201,12 @@ - (UIImage *)createBitmapSVGWithData:(nonnull NSData *)data targetSize:(CGSize)t
205201
CGContextConcatCTM(context, translationTransform);
206202
CGContextConcatCTM(context, scaleTransform);
207203

208-
CGContextDrawSVGDocument(context, document);
204+
SDCGContextDrawSVGDocument(context, document);
209205

210206
image = SDGraphicsGetImageFromCurrentImageContext();
211207
SDGraphicsEndImageContext();
212208

213-
CGSVGDocumentRelease(document);
209+
SDCGSVGDocumentRelease(document);
214210

215211
return image;
216212
}
@@ -223,14 +219,14 @@ + (BOOL)supportsVectorSVGImage {
223219
dispatch_once(&onceToken, ^{
224220
#if SD_MAC
225221
// macOS 10.15+ supports SVG built-in rendering, use selector to check is more accurate
226-
if (NSClassFromString(NSSVGImageRepClass)) {
222+
if (SDNSSVGImageRepClass) {
227223
supports = YES;
228224
} else {
229225
supports = NO;
230226
}
231227
#else
232228
// iOS 13+ supports SVG built-in rendering, use selector to check is more accurate
233-
if ([UIImage respondsToSelector:@selector(_imageWithCGSVGDocument:)]) {
229+
if ([UIImage respondsToSelector:SDImageWithCGSVGDocumentSEL]) {
234230
supports = YES;
235231
} else {
236232
supports = NO;

0 commit comments

Comments
 (0)