Skip to content
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

Render by PDFKit on iOS #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
46 changes: 35 additions & 11 deletions SDWebImagePDFCoder/Classes/SDImagePDFCoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#import "SDImagePDFCoder.h"
#import "SDWebImagePDFCoderDefine.h"
#import "objc/runtime.h"
#import <PDFKit/PDFKit.h>

#define SD_FOUR_CC(c1,c2,c3,c4) ((uint32_t)(((c4) << 24) | ((c3) << 16) | ((c2) << 8) | (c1)))

Expand Down Expand Up @@ -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();

Comment on lines +182 to +196
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Investigate the undefined targetRect.
SDGraphicsBeginImageContextWithOptions(targetRect.size, NO, 0); relies on targetRect, which is never declared or assigned before use. This likely causes undefined behavior or a compilation error.

Apply this diff to correct usage by substituting rect:

-    SDGraphicsBeginImageContextWithOptions(targetRect.size, NO, 0);
+    SDGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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();
SDGraphicsBeginImageContextWithOptions(rect.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;
Expand Down