diff --git a/SDWebImagePDFCoder.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/SDWebImagePDFCoder.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/SDWebImagePDFCoder.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/SDWebImagePDFCoder/Classes/SDImagePDFCoder.m b/SDWebImagePDFCoder/Classes/SDImagePDFCoder.m index aeab260..5f8ae9f 100644 --- a/SDWebImagePDFCoder/Classes/SDImagePDFCoder.m +++ b/SDWebImagePDFCoder/Classes/SDImagePDFCoder.m @@ -8,6 +8,7 @@ #import "SDImagePDFCoder.h" #import "SDWebImagePDFCoderDefine.h" #import "objc/runtime.h" +#import #define SD_FOUR_CC(c1,c2,c3,c4) ((uint32_t)(((c4) << 24) | ((c3) << 16) | ((c2) << 8) | (c1))) @@ -151,25 +152,48 @@ - (UIImage *)createVectorPDFWithData:(nonnull NSData *)data pageNumber:(NSUInteg image = [[NSImage alloc] initWithSize:imageRep.size]; [image addRepresentation:imageRep]; #else - CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data); - if (!provider) { + + PDFDocument *document = [[PDFDocument alloc]initWithData:data]; + if (!document) { return nil; } - CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(provider); - CGDataProviderRelease(provider); - if (!document) { + if (pageNumber >= document.pageCount) { return nil; } - - // `CGPDFDocumentGetPage` page number is 1-indexed. - CGPDFPageRef page = CGPDFDocumentGetPage(document, pageNumber + 1); + PDFPage *page = [document pageAtIndex:pageNumber]; if (!page) { - CGPDFDocumentRelease(document); return nil; } - image = ((UIImage *(*)(id,SEL,CGPDFPageRef))[UIImage.class methodForSelector:SDImageWithCGPDFPageSEL])(UIImage.class, SDImageWithCGPDFPageSEL, page); - CGPDFDocumentRelease(document); + CGPDFDocumentRef documentRef = document.documentRef; + if (!documentRef) { + return nil; + } + + CGPDFPageRef pageRef = page.pageRef; + if (!pageRef) { + return nil; + } + + CGPDFBox box = kCGPDFMediaBox; + CGRect rect = CGPDFPageGetBoxRect(pageRef, box); + CGAffineTransform transform = CGPDFPageGetDrawingTransform(pageRef, box, rect, 0, YES); + + SDGraphicsBeginImageContextWithOptions(targetRect.size, NO, 0); + CGContextRef context = SDGraphicsGetCurrentContext(); + +#if SD_UIKIT || SD_WATCH + // Core Graphics coordinate system use the bottom-left, UIKit use the flipped one + CGContextTranslateCTM(context, 0, rect.size.height); + CGContextScaleCTM(context, 1, -1); +#endif + + CGContextConcatCTM(context, transform); + [page drawWithBox:kPDFDisplayBoxMediaBox toContext:context]; + + image = SDGraphicsGetImageFromCurrentImageContext(); + SDGraphicsEndImageContext(); + #endif return image;