Skip to content

Changing the PDF symbol naming with the encoded string instead #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Carthage/Build
# `pod install` in .travis.yml
#
Pods/
Podfile.lock

# SwiftPM
.swiftpm
Expand Down
25 changes: 0 additions & 25 deletions Example/Podfile.lock

This file was deleted.

8 changes: 0 additions & 8 deletions Example/SDWebImagePDFCoder.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -386,16 +386,12 @@
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-SDWebImagePDFCoder_Example macOS/Pods-SDWebImagePDFCoder_Example macOS-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/SDWebImage-macOS/SDWebImage.framework",
"${BUILT_PRODUCTS_DIR}/SDWebImagePDFCoder-macOS/SDWebImagePDFCoder.framework",
);
name = "[CP] Embed Pods Frameworks";
outputFileListPaths = (
);
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SDWebImage.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SDWebImagePDFCoder.framework",
Expand All @@ -410,16 +406,12 @@
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-SDWebImagePDFCoder_Example/Pods-SDWebImagePDFCoder_Example-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/SDWebImage-iOS/SDWebImage.framework",
"${BUILT_PRODUCTS_DIR}/SDWebImagePDFCoder-iOS/SDWebImagePDFCoder.framework",
);
name = "[CP] Embed Pods Frameworks";
outputFileListPaths = (
);
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SDWebImage.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SDWebImagePDFCoder.framework",
Expand Down

This file was deleted.

31 changes: 19 additions & 12 deletions SDWebImagePDFCoder/Classes/SDImagePDFCoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,27 @@
#define SD_FOUR_CC(c1,c2,c3,c4) ((uint32_t)(((c4) << 24) | ((c3) << 16) | ((c2) << 8) | (c1)))

#if SD_UIKIT || SD_WATCH
// iOS/tvOS 11+ UIImage add built-in vector PDF image support. So we use that instead of drawing bitmap image
@interface UIImage (PrivatePDFSupport)
static SEL SDImageWithCGPDFPageSEL = NULL;
static SEL SDCGPDFPageSEL = NULL;

- (instancetype)_initWithCGPDFPage:(CGPDFPageRef)page;
- (instancetype)_initWithCGPDFPage:(CGPDFPageRef)page scale:(double)scale orientation:(UIImageOrientation)orientation;
+ (instancetype)_imageWithCGPDFPage:(CGPDFPageRef)page;
+ (instancetype)_imageWithCGPDFPage:(CGPDFPageRef)page scale:(double)scale orientation:(UIImageOrientation)orientation;
- (CGPDFPageRef)_CGPDFPage;

@end
static inline NSString *SDBase64DecodedString(NSString *base64String) {
NSData *data = [[NSData alloc] initWithBase64EncodedString:base64String options:NSDataBase64DecodingIgnoreUnknownCharacters];
if (!data) {
return nil;
}
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
#endif

@implementation SDImagePDFCoder

#if SD_UIKIT || SD_WATCH
+ (void)initialize {
SDImageWithCGPDFPageSEL = NSSelectorFromString(SDBase64DecodedString(@"X2ltYWdlV2l0aENHUERGUGFnZTo="));
SDCGPDFPageSEL = NSSelectorFromString(SDBase64DecodedString(@"X0NHUERGUGFnZQ=="));
}
#endif

+ (SDImagePDFCoder *)sharedCoder {
static dispatch_once_t onceToken;
static SDImagePDFCoder *coder;
Expand Down Expand Up @@ -98,7 +105,7 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o
}
return ((NSPDFImageRep *)imageRep).PDFRepresentation;
#else
CGPDFPageRef page = [image _CGPDFPage];
CGPDFPageRef page = ((CGPDFPageRef (*)(id,SEL))[image methodForSelector:SDCGPDFPageSEL])(image, SDCGPDFPageSEL);
if (!page) {
return nil;
}
Expand Down Expand Up @@ -154,7 +161,7 @@ - (UIImage *)createVectorPDFWithData:(nonnull NSData *)data pageNumber:(NSUInteg
return nil;
}

image = [UIImage _imageWithCGPDFPage:page];
image = ((UIImage *(*)(id,SEL,CGPDFPageRef))[UIImage.class methodForSelector:SDImageWithCGPDFPageSEL])(UIImage.class, SDImageWithCGPDFPageSEL, page);
CGPDFDocumentRelease(document);
#endif

Expand Down Expand Up @@ -231,7 +238,7 @@ + (BOOL)supportsVectorPDFImage {
static BOOL supports;
dispatch_once(&onceToken, ^{
// iOS 11+ supports PDF built-in rendering, use selector to check is more accurate
if ([UIImage respondsToSelector:@selector(_imageWithCGPDFPage:)]) {
if ([UIImage respondsToSelector:SDImageWithCGPDFPageSEL]) {
supports = YES;
} else {
supports = NO;
Expand Down