@@ -49,12 +49,12 @@ + (SDImageSVGCoder *)sharedCoder {
49
49
}
50
50
51
51
+ (void )initialize {
52
- SDCGSVGDocumentRetain = dlsym (RTLD_DEFAULT, SDBase64DecodedString (@" Q0dTVkdEb2N1bWVudFJldGFpbg==" ).UTF8String );
53
- SDCGSVGDocumentRelease = dlsym (RTLD_DEFAULT, SDBase64DecodedString (@" Q0dTVkdEb2N1bWVudFJlbGVhc2U=" ).UTF8String );
54
- SDCGSVGDocumentCreateFromData = dlsym (RTLD_DEFAULT, SDBase64DecodedString (@" Q0dTVkdEb2N1bWVudENyZWF0ZUZyb21EYXRh" ).UTF8String );
55
- SDCGSVGDocumentWriteToData = dlsym (RTLD_DEFAULT, SDBase64DecodedString (@" Q0dTVkdEb2N1bWVudFdyaXRlVG9EYXRh" ).UTF8String );
56
- SDCGContextDrawSVGDocument = dlsym (RTLD_DEFAULT, SDBase64DecodedString (@" Q0dDb250ZXh0RHJhd1NWR0RvY3VtZW50" ).UTF8String );
57
- SDCGSVGDocumentGetCanvasSize = dlsym (RTLD_DEFAULT, SDBase64DecodedString (@" Q0dTVkdEb2N1bWVudEdldENhbnZhc1NpemU=" ).UTF8String );
52
+ SDCGSVGDocumentRetain = ( CGSVGDocumentRef (*)(CGSVGDocumentRef)) dlsym (RTLD_DEFAULT, SDBase64DecodedString (@" Q0dTVkdEb2N1bWVudFJldGFpbg==" ).UTF8String );
53
+ SDCGSVGDocumentRelease = ( void (*)(CGSVGDocumentRef)) dlsym (RTLD_DEFAULT, SDBase64DecodedString (@" Q0dTVkdEb2N1bWVudFJlbGVhc2U=" ).UTF8String );
54
+ SDCGSVGDocumentCreateFromData = ( CGSVGDocumentRef (*)( CFDataRef data, CFDictionaryRef options)) dlsym (RTLD_DEFAULT, SDBase64DecodedString (@" Q0dTVkdEb2N1bWVudENyZWF0ZUZyb21EYXRh" ).UTF8String );
55
+ SDCGSVGDocumentWriteToData = ( void (*)(CGSVGDocumentRef document, CFDataRef data, CFDictionaryRef options)) dlsym (RTLD_DEFAULT, SDBase64DecodedString (@" Q0dTVkdEb2N1bWVudFdyaXRlVG9EYXRh" ).UTF8String );
56
+ SDCGContextDrawSVGDocument = ( void (*)( CGContextRef context, CGSVGDocumentRef document)) dlsym (RTLD_DEFAULT, SDBase64DecodedString (@" Q0dDb250ZXh0RHJhd1NWR0RvY3VtZW50" ).UTF8String );
57
+ SDCGSVGDocumentGetCanvasSize = ( CGSize (*)(CGSVGDocumentRef document)) dlsym (RTLD_DEFAULT, SDBase64DecodedString (@" Q0dTVkdEb2N1bWVudEdldENhbnZhc1NpemU=" ).UTF8String );
58
58
#if SD_UIKIT || SD_WATCH
59
59
SDImageWithCGSVGDocumentSEL = NSSelectorFromString (SDBase64DecodedString (@" X2ltYWdlV2l0aENHU1ZHRG9jdW1lbnQ6" ));
60
60
SDCGSVGDocumentSEL = NSSelectorFromString (SDBase64DecodedString (@" X0NHU1ZHRG9jdW1lbnQ=" ));
@@ -152,7 +152,15 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o
152
152
return nil ;
153
153
}
154
154
155
- SDCGSVGDocumentWriteToData (document, (__bridge CFDataRef )data, NULL );
155
+ @try {
156
+ // WARNING! Some CoreSVG exceptions can be catched, but not always
157
+ // If you finally crash here (un-catchable), you can only workaround (or hope Apple fix this)
158
+ // Do not encode vector UIImage into NSData, query `SDImageCache` for the same key and get back SVG Data
159
+ SDCGSVGDocumentWriteToData (document, (__bridge CFMutableDataRef )data, NULL );
160
+ } @catch (...) {
161
+ // CoreSVG export failed
162
+ return nil ;
163
+ }
156
164
157
165
return [data copy ];
158
166
}
@@ -178,6 +186,23 @@ - (UIImage *)createVectorSVGWithData:(nonnull NSData *)data {
178
186
image = ((UIImage *(*)(id ,SEL ,CGSVGDocumentRef))[UIImage.class methodForSelector: SDImageWithCGSVGDocumentSEL])(UIImage.class , SDImageWithCGSVGDocumentSEL, document);
179
187
SDCGSVGDocumentRelease (document);
180
188
#endif
189
+
190
+ // CoreSVG has compatible for some SVG/1.1 format (like Font issue) and may crash when rendering on screen (not here, Core Animation commit time)
191
+ // So, we snapshot a 1x1 pixel image and try catch here to check :(
192
+
193
+ SDGraphicsImageRenderer *renderer = [[SDGraphicsImageRenderer alloc ] initWithSize: CGSizeMake (1 , 1 )];
194
+ @try {
195
+ __unused UIImage *dummyImage = [renderer imageWithActions: ^(CGContextRef _Nonnull context) {
196
+ // WARNING! Some CoreSVG exceptions can be catched, but not always
197
+ // If you finally crash here (un-catchable), you can only workaround (or hope Apple fix this)
198
+ // Change your code to use `SDWebImageContextImageThumbnailPixelSize` context option with enough size to render bitmap SVG instead
199
+ [image drawInRect: CGRectMake (0 , 0 , 1 , 1 )];
200
+ }];
201
+ } @catch (...) {
202
+ // CoreSVG decode failed
203
+ return nil ;
204
+ }
205
+
181
206
return image;
182
207
}
183
208
0 commit comments