|
11 | 11 |
|
12 | 12 | #define SD_FOUR_CC(c1,c2,c3,c4) ((uint32_t)(((c4) << 24) | ((c3) << 16) | ((c2) << 8) | (c1)))
|
13 | 13 |
|
| 14 | +@interface SDImageIOCoder () |
| 15 | + |
| 16 | +// From SDWebImage 5.14.1 |
| 17 | ++ (UIImage *)createBitmapPDFWithData:(nonnull NSData *)data pageNumber:(NSUInteger)pageNumber targetSize:(CGSize)targetSize preserveAspectRatio:(BOOL)preserveAspectRatio; |
| 18 | + |
| 19 | +@end |
| 20 | + |
14 | 21 | #if SD_UIKIT || SD_WATCH
|
15 | 22 | static SEL SDImageWithCGPDFPageSEL = NULL;
|
16 | 23 | static SEL SDCGPDFPageSEL = NULL;
|
@@ -56,46 +63,30 @@ - (UIImage *)decodedImageWithData:(NSData *)data options:(SDImageCoderOptions *)
|
56 | 63 | CGSize imageSize = CGSizeZero;
|
57 | 64 | BOOL preserveAspectRatio = YES;
|
58 | 65 |
|
59 |
| -#pragma clang diagnostic push |
60 |
| -#pragma clang diagnostic ignored "-Wdeprecated-declarations" |
61 | 66 | // Parse args
|
62 |
| - SDWebImageContext *context = options[SDImageCoderWebImageContext]; |
63 |
| - if (context[SDWebImageContextPDFPageNumber]) { |
64 |
| - pageNumber = [context[SDWebImageContextPDFPageNumber] unsignedIntegerValue]; |
65 |
| - } else if (options[SDImageCoderDecodePDFPageNumber]) { |
| 67 | + if (options[SDImageCoderDecodePDFPageNumber]) { |
66 | 68 | pageNumber = [options[SDImageCoderDecodePDFPageNumber] unsignedIntegerValue];
|
67 | 69 | }
|
68 |
| - if (context[SDWebImageContextPDFImageSize]) { |
69 |
| - prefersBitmap = YES; |
70 |
| - NSValue *sizeValue = context[SDWebImageContextPDFImageSize]; |
71 |
| -#if SD_MAC |
72 |
| - imageSize = sizeValue.sizeValue; |
73 |
| -#else |
74 |
| - imageSize = sizeValue.CGSizeValue; |
75 |
| -#endif |
76 |
| - } else if (options[SDImageCoderDecodeThumbnailPixelSize]) { |
| 70 | + if (options[SDImageCoderDecodeThumbnailPixelSize]) { |
77 | 71 | prefersBitmap = YES;
|
78 | 72 | NSValue *sizeValue = options[SDImageCoderDecodeThumbnailPixelSize];
|
79 | 73 | #if SD_MAC
|
80 | 74 | imageSize = sizeValue.sizeValue;
|
81 | 75 | #else
|
82 | 76 | imageSize = sizeValue.CGSizeValue;
|
83 | 77 | #endif
|
84 |
| - } else if (context[SDWebImageContextPDFPrefersBitmap]) { |
85 |
| - prefersBitmap = [context[SDWebImageContextPDFPrefersBitmap] boolValue]; |
86 | 78 | }
|
87 |
| - if (context[SDWebImageContextPDFImagePreserveAspectRatio]) { |
88 |
| - preserveAspectRatio = [context[SDWebImageContextPDFImagePreserveAspectRatio] boolValue]; |
89 |
| - } else if (options[SDImageCoderDecodePreserveAspectRatio]) { |
90 |
| - preserveAspectRatio = [context[SDImageCoderDecodePreserveAspectRatio] boolValue]; |
| 79 | + if (options[SDImageCoderDecodePreserveAspectRatio]) { |
| 80 | + preserveAspectRatio = [options[SDImageCoderDecodePreserveAspectRatio] boolValue]; |
91 | 81 | }
|
92 | 82 | #pragma clang diagnostic pop
|
93 | 83 |
|
94 | 84 | UIImage *image;
|
95 | 85 | if (!prefersBitmap && [self.class supportsVectorPDFImage]) {
|
96 | 86 | image = [self createVectorPDFWithData:data pageNumber:pageNumber];
|
97 | 87 | } else {
|
98 |
| - image = [self createBitmapPDFWithData:data pageNumber:pageNumber targetSize:imageSize preserveAspectRatio:preserveAspectRatio]; |
| 88 | + NSAssert([SDImageIOCoder respondsToSelector:@selector(createBitmapPDFWithData:pageNumber:targetSize:preserveAspectRatio:)], @"SDWebImage from 5.14.1 should contains this API"); |
| 89 | + image = [SDImageIOCoder createBitmapPDFWithData:data pageNumber:pageNumber targetSize:imageSize preserveAspectRatio:preserveAspectRatio]; |
99 | 90 | }
|
100 | 91 |
|
101 | 92 | image.sd_imageFormat = SDImageFormatPDF;
|
@@ -184,67 +175,6 @@ - (UIImage *)createVectorPDFWithData:(nonnull NSData *)data pageNumber:(NSUInteg
|
184 | 175 | return image;
|
185 | 176 | }
|
186 | 177 |
|
187 |
| -#pragma mark - Bitmap PDF representation |
188 |
| -- (UIImage *)createBitmapPDFWithData:(nonnull NSData *)data pageNumber:(NSUInteger)pageNumber targetSize:(CGSize)targetSize preserveAspectRatio:(BOOL)preserveAspectRatio { |
189 |
| - NSParameterAssert(data); |
190 |
| - UIImage *image; |
191 |
| - |
192 |
| - CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data); |
193 |
| - if (!provider) { |
194 |
| - return nil; |
195 |
| - } |
196 |
| - CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(provider); |
197 |
| - CGDataProviderRelease(provider); |
198 |
| - if (!document) { |
199 |
| - return nil; |
200 |
| - } |
201 |
| - |
202 |
| - // `CGPDFDocumentGetPage` page number is 1-indexed. |
203 |
| - CGPDFPageRef page = CGPDFDocumentGetPage(document, pageNumber + 1); |
204 |
| - if (!page) { |
205 |
| - CGPDFDocumentRelease(document); |
206 |
| - return nil; |
207 |
| - } |
208 |
| - |
209 |
| - CGPDFBox box = kCGPDFMediaBox; |
210 |
| - CGRect rect = CGPDFPageGetBoxRect(page, box); |
211 |
| - CGRect targetRect = rect; |
212 |
| - if (!CGSizeEqualToSize(targetSize, CGSizeZero)) { |
213 |
| - targetRect = CGRectMake(0, 0, targetSize.width, targetSize.height); |
214 |
| - } |
215 |
| - |
216 |
| - CGFloat xRatio = targetRect.size.width / rect.size.width; |
217 |
| - CGFloat yRatio = targetRect.size.height / rect.size.height; |
218 |
| - CGFloat xScale = preserveAspectRatio ? MIN(xRatio, yRatio) : xRatio; |
219 |
| - CGFloat yScale = preserveAspectRatio ? MIN(xRatio, yRatio) : yRatio; |
220 |
| - |
221 |
| - // CGPDFPageGetDrawingTransform will only scale down, but not scale up, so we need calculcate the actual scale again |
222 |
| - CGRect drawRect = CGRectMake( 0, 0, targetRect.size.width / xScale, targetRect.size.height / yScale); |
223 |
| - CGAffineTransform scaleTransform = CGAffineTransformMakeScale(xScale, yScale); |
224 |
| - CGAffineTransform transform = CGPDFPageGetDrawingTransform(page, box, drawRect, 0, preserveAspectRatio); |
225 |
| - |
226 |
| - SDGraphicsBeginImageContextWithOptions(targetRect.size, NO, 0); |
227 |
| - CGContextRef context = SDGraphicsGetCurrentContext(); |
228 |
| - |
229 |
| -#if SD_UIKIT || SD_WATCH |
230 |
| - // Core Graphics coordinate system use the bottom-left, UIkit use the flipped one |
231 |
| - CGContextTranslateCTM(context, 0, targetRect.size.height); |
232 |
| - CGContextScaleCTM(context, 1, -1); |
233 |
| -#endif |
234 |
| - |
235 |
| - CGContextConcatCTM(context, scaleTransform); |
236 |
| - CGContextConcatCTM(context, transform); |
237 |
| - |
238 |
| - CGContextDrawPDFPage(context, page); |
239 |
| - |
240 |
| - image = SDGraphicsGetImageFromCurrentImageContext(); |
241 |
| - SDGraphicsEndImageContext(); |
242 |
| - |
243 |
| - CGPDFDocumentRelease(document); |
244 |
| - |
245 |
| - return image; |
246 |
| -} |
247 |
| - |
248 | 178 | + (BOOL)supportsVectorPDFImage {
|
249 | 179 | #if SD_MAC
|
250 | 180 | // macOS's `NSImage` supports PDF built-in rendering
|
|
0 commit comments