@@ -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,12 @@ - (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
+ SDCGSVGDocumentWriteToData (document, (__bridge CFMutableDataRef)data, NULL );
157
+ } @catch (...) {
158
+ // CoreSVG export failed
159
+ return nil ;
160
+ }
156
161
157
162
return [data copy ];
158
163
}
@@ -178,6 +183,23 @@ - (UIImage *)createVectorSVGWithData:(nonnull NSData *)data {
178
183
image = ((UIImage *(*)(id ,SEL ,CGSVGDocumentRef))[UIImage.class methodForSelector: SDImageWithCGSVGDocumentSEL])(UIImage.class , SDImageWithCGSVGDocumentSEL, document);
179
184
SDCGSVGDocumentRelease (document);
180
185
#endif
186
+
187
+ // 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)
188
+ // So, we snapshot a 1x1 pixel image and try catch here to check :(
189
+
190
+ SDGraphicsImageRenderer *renderer = [[SDGraphicsImageRenderer alloc ] initWithSize: CGSizeMake (1 , 1 )];
191
+ @try {
192
+ __unused UIImage *dummyImage = [renderer imageWithActions: ^(CGContextRef _Nonnull context) {
193
+ // WARNING! Some CoreSVG exceptions can be catched, but not always
194
+ // If you finally crash here (un-catchable), you can only workaround (or hope Apple fix this)
195
+ // Change your code to use `SDWebImageContextImageThumbnailPixelSize` context option with enough size to render bitmap SVG instead
196
+ [image drawInRect: CGRectMake (0 , 0 , 1 , 1 )];
197
+ }];
198
+ } @catch (...) {
199
+ // CoreSVG decode failed
200
+ return nil ;
201
+ }
202
+
181
203
return image;
182
204
}
183
205
0 commit comments