Skip to content

Commit cc5822c

Browse files
committed
fix render pdf annotation
1 parent d913966 commit cc5822c

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

SDWebImagePDFCoder/Classes/SDImagePDFCoder.m

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#import "SDImagePDFCoder.h"
99
#import "SDWebImagePDFCoderDefine.h"
1010
#import "objc/runtime.h"
11+
#import <PDFKit/PDFKit.h>
1112

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

@@ -151,25 +152,48 @@ - (UIImage *)createVectorPDFWithData:(nonnull NSData *)data pageNumber:(NSUInteg
151152
image = [[NSImage alloc] initWithSize:imageRep.size];
152153
[image addRepresentation:imageRep];
153154
#else
154-
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
155-
if (!provider) {
155+
156+
PDFDocument *document = [[PDFDocument alloc]initWithData:data];
157+
if (!document) {
156158
return nil;
157159
}
158-
CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(provider);
159-
CGDataProviderRelease(provider);
160-
if (!document) {
160+
if (pageNumber >= document.pageCount) {
161161
return nil;
162162
}
163-
164-
// `CGPDFDocumentGetPage` page number is 1-indexed.
165-
CGPDFPageRef page = CGPDFDocumentGetPage(document, pageNumber + 1);
163+
PDFPage *page = [document pageAtIndex:pageNumber];
166164
if (!page) {
167-
CGPDFDocumentRelease(document);
168165
return nil;
169166
}
170167

171-
image = ((UIImage *(*)(id,SEL,CGPDFPageRef))[UIImage.class methodForSelector:SDImageWithCGPDFPageSEL])(UIImage.class, SDImageWithCGPDFPageSEL, page);
172-
CGPDFDocumentRelease(document);
168+
CGPDFDocumentRef documentRef = document.documentRef;
169+
if (!documentRef) {
170+
return nil;
171+
}
172+
173+
CGPDFPageRef pageRef = page.pageRef;
174+
if (!pageRef) {
175+
return nil;
176+
}
177+
178+
CGPDFBox box = kCGPDFMediaBox;
179+
CGRect rect = CGPDFPageGetBoxRect(pageRef, box);
180+
CGAffineTransform transform = CGPDFPageGetDrawingTransform(pageRef, box, rect, 0, YES);
181+
182+
SDGraphicsBeginImageContextWithOptions(targetRect.size, NO, 0);
183+
CGContextRef context = SDGraphicsGetCurrentContext();
184+
185+
#if SD_UIKIT || SD_WATCH
186+
// Core Graphics coordinate system use the bottom-left, UIKit use the flipped one
187+
CGContextTranslateCTM(context, 0, rect.size.height);
188+
CGContextScaleCTM(context, 1, -1);
189+
#endif
190+
191+
CGContextConcatCTM(context, transform);
192+
[page drawWithBox:kPDFDisplayBoxMediaBox toContext:context];
193+
194+
image = SDGraphicsGetImageFromCurrentImageContext();
195+
SDGraphicsEndImageContext();
196+
173197
#endif
174198

175199
return image;

0 commit comments

Comments
 (0)