Skip to content

Commit 429986f

Browse files
committed
Change from using NSObject performSelector, into the IMP call, which is more safe on memory management and fast. Change the Ivar instead of String as well for performance
1 parent 1831d73 commit 429986f

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

SDWebImageSVGCoder/Classes/SDImageSVGCoder.m

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#endif
2727
#if SD_MAC
2828
static Class SDNSSVGImageRepClass = NULL;
29+
static Ivar SDNSSVGImageRepDocumentIvar = NULL;
2930
#endif
3031

3132
static inline NSString *SDBase64DecodedString(NSString *base64String) {
@@ -60,6 +61,9 @@ + (void)initialize {
6061
#endif
6162
#if SD_MAC
6263
SDNSSVGImageRepClass = NSClassFromString(SDBase64DecodedString(@"X05TU1ZHSW1hZ2VSZXA="));
64+
if (SDNSSVGImageRepClass) {
65+
SDNSSVGImageRepDocumentIvar = class_getInstanceVariable(SDNSSVGImageRepClass, SDBase64DecodedString(@"X2RvY3VtZW50").UTF8String);
66+
}
6367
#endif
6468
}
6569

@@ -125,12 +129,10 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o
125129
NSRect imageRect = NSMakeRect(0, 0, image.size.width, image.size.height);
126130
NSImageRep *imageRep = [image bestRepresentationForRect:imageRect context:nil hints:nil];
127131
if ([imageRep isKindOfClass:SDNSSVGImageRepClass]) {
128-
Ivar ivar = class_getInstanceVariable(imageRep.class, SDBase64DecodedString(@"X2RvY3VtZW50").UTF8String);
129-
document = (__bridge CGSVGDocumentRef)(object_getIvar(imageRep, ivar));
132+
document = (__bridge CGSVGDocumentRef)(object_getIvar(imageRep, SDNSSVGImageRepDocumentIvar));
130133
}
131134
#else
132-
CGSVGDocumentRef (*method)(id,SEL) = (CGSVGDocumentRef (*)(id,SEL))[image methodForSelector:SDCGSVGDocumentSEL];
133-
document = method(image, SDCGSVGDocumentSEL);
135+
document = ((CGSVGDocumentRef (*)(id,SEL))[image methodForSelector:SDCGSVGDocumentSEL])(image, SDCGSVGDocumentSEL);
134136
#endif
135137
if (!document) {
136138
return nil;
@@ -159,7 +161,7 @@ - (UIImage *)createVectorSVGWithData:(nonnull NSData *)data {
159161
if (!document) {
160162
return nil;
161163
}
162-
image = [UIImage performSelector:SDImageWithCGSVGDocumentSEL withObject:(__bridge id)(document)];
164+
image = ((UIImage *(*)(id,SEL,CGSVGDocumentRef))[UIImage.class methodForSelector:SDImageWithCGSVGDocumentSEL])(UIImage.class, SDImageWithCGSVGDocumentSEL, document);
163165
SDCGSVGDocumentRelease(document);
164166
#endif
165167
return image;

0 commit comments

Comments
 (0)