|
12 | 12 | #define SD_FOUR_CC(c1,c2,c3,c4) ((uint32_t)(((c4) << 24) | ((c3) << 16) | ((c2) << 8) | (c1))) |
13 | 13 |
|
14 | 14 | #if SD_UIKIT || SD_WATCH |
15 | | -// iOS/tvOS 11+ UIImage add built-in vector PDF image support. So we use that instead of drawing bitmap image |
16 | | -@interface UIImage (PrivatePDFSupport) |
| 15 | +static SEL SDImageWithCGPDFPageSEL = NULL; |
| 16 | +static SEL SDCGPDFPageSEL = NULL; |
17 | 17 |
|
18 | | -- (instancetype)_initWithCGPDFPage:(CGPDFPageRef)page; |
19 | | -- (instancetype)_initWithCGPDFPage:(CGPDFPageRef)page scale:(double)scale orientation:(UIImageOrientation)orientation; |
20 | | -+ (instancetype)_imageWithCGPDFPage:(CGPDFPageRef)page; |
21 | | -+ (instancetype)_imageWithCGPDFPage:(CGPDFPageRef)page scale:(double)scale orientation:(UIImageOrientation)orientation; |
22 | | -- (CGPDFPageRef)_CGPDFPage; |
23 | | - |
24 | | -@end |
| 18 | +static inline NSString *SDBase64DecodedString(NSString *base64String) { |
| 19 | + NSData *data = [[NSData alloc] initWithBase64EncodedString:base64String options:NSDataBase64DecodingIgnoreUnknownCharacters]; |
| 20 | + if (!data) { |
| 21 | + return nil; |
| 22 | + } |
| 23 | + return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; |
| 24 | +} |
25 | 25 | #endif |
26 | 26 |
|
27 | 27 | @implementation SDImagePDFCoder |
28 | 28 |
|
| 29 | +#if SD_UIKIT || SD_WATCH |
| 30 | ++ (void)initialize { |
| 31 | + SDImageWithCGPDFPageSEL = NSSelectorFromString(SDBase64DecodedString(@"X2ltYWdlV2l0aENHUERGUGFnZTo=")); |
| 32 | + SDCGPDFPageSEL = NSSelectorFromString(SDBase64DecodedString(@"X0NHUERGUGFnZQ==")); |
| 33 | +} |
| 34 | +#endif |
| 35 | + |
29 | 36 | + (SDImagePDFCoder *)sharedCoder { |
30 | 37 | static dispatch_once_t onceToken; |
31 | 38 | static SDImagePDFCoder *coder; |
@@ -98,7 +105,7 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o |
98 | 105 | } |
99 | 106 | return ((NSPDFImageRep *)imageRep).PDFRepresentation; |
100 | 107 | #else |
101 | | - CGPDFPageRef page = [image _CGPDFPage]; |
| 108 | + CGPDFPageRef page = ((CGPDFPageRef (*)(id,SEL))[image methodForSelector:SDCGPDFPageSEL])(image, SDCGPDFPageSEL); |
102 | 109 | if (!page) { |
103 | 110 | return nil; |
104 | 111 | } |
@@ -154,7 +161,7 @@ - (UIImage *)createVectorPDFWithData:(nonnull NSData *)data pageNumber:(NSUInteg |
154 | 161 | return nil; |
155 | 162 | } |
156 | 163 |
|
157 | | - image = [UIImage _imageWithCGPDFPage:page]; |
| 164 | + image = ((UIImage *(*)(id,SEL,CGPDFPageRef))[UIImage.class methodForSelector:SDImageWithCGPDFPageSEL])(UIImage.class, SDImageWithCGPDFPageSEL, page); |
158 | 165 | CGPDFDocumentRelease(document); |
159 | 166 | #endif |
160 | 167 |
|
@@ -231,7 +238,7 @@ + (BOOL)supportsVectorPDFImage { |
231 | 238 | static BOOL supports; |
232 | 239 | dispatch_once(&onceToken, ^{ |
233 | 240 | // iOS 11+ supports PDF built-in rendering, use selector to check is more accurate |
234 | | - if ([UIImage respondsToSelector:@selector(_imageWithCGPDFPage:)]) { |
| 241 | + if ([UIImage respondsToSelector:SDImageWithCGPDFPageSEL]) { |
235 | 242 | supports = YES; |
236 | 243 | } else { |
237 | 244 | supports = NO; |
|
0 commit comments