Skip to content

Commit 8fe4bc5

Browse files
committed
Upgrade dependency of SDWebImage to 5.14.1+
Move the bitmap decode from this library to the Core
1 parent aec21a3 commit 8fe4bc5

File tree

6 files changed

+28
-125
lines changed

6 files changed

+28
-125
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>PreviewsEnabled</key>
6+
<false/>
7+
</dict>
8+
</plist>

Example/SDWebImagePDFCoder/SDViewController.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ - (void)viewDidLoad
6767
[imageView3 sd_setImageWithURL:pdfURL3 placeholderImage:nil options:SDWebImageRetryFailed context:@{SDWebImageContextImageThumbnailPixelSize: @(CGSizeMake(100, 100))} progress:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
6868
if (image) {
6969
NSLog(@"PDF bitmap load success.");
70-
NSData *svgData = [image sd_imageDataAsFormat:SDImageFormatPDF];
71-
NSAssert(!svgData, @"SVG Data should not exist");
70+
NSData *pdfData = [image sd_imageDataAsFormat:SDImageFormatPDF];
71+
NSAssert(!pdfData, @"PDF Data should not exist");
7272
}
7373
}];
7474
}

SDWebImagePDFCoder.podspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'SDWebImagePDFCoder'
11-
s.version = '0.8.0'
11+
s.version = '1.0.0'
1212
s.summary = 'A PDF coder plugin for SDWebImage, using built-in framework'
1313

1414
# This description is used to generate tags and improve search results.
@@ -18,7 +18,7 @@ Pod::Spec.new do |s|
1818
# * Finally, don't worry about the indent, CocoaPods strips it!
1919

2020
s.description = <<-DESC
21-
SDWebImageSVGCoder is a SVG coder plugin for SDWebImage framework, which provide the image loading support for SVG using SVGKit SVG engine.
21+
SDWebImagePDFCoder is a PDF coder plugin for SDWebImage framework.
2222
DESC
2323

2424
s.homepage = 'https://github.com/SDWebImage/SDWebImagePDFCoder'
@@ -39,5 +39,5 @@ SDWebImageSVGCoder is a SVG coder plugin for SDWebImage framework, which provide
3939
'DERIVE_MACCATALYST_PRODUCT_BUNDLE_IDENTIFIER' => 'NO'
4040
}
4141

42-
s.dependency 'SDWebImage', '~> 5.10'
42+
s.dependency 'SDWebImage', '>= 5.14.1'
4343
end

SDWebImagePDFCoder/Classes/SDImagePDFCoder.m

Lines changed: 13 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111

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

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+
1421
#if SD_UIKIT || SD_WATCH
1522
static SEL SDImageWithCGPDFPageSEL = NULL;
1623
static SEL SDCGPDFPageSEL = NULL;
@@ -56,46 +63,30 @@ - (UIImage *)decodedImageWithData:(NSData *)data options:(SDImageCoderOptions *)
5663
CGSize imageSize = CGSizeZero;
5764
BOOL preserveAspectRatio = YES;
5865

59-
#pragma clang diagnostic push
60-
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
6166
// 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]) {
6668
pageNumber = [options[SDImageCoderDecodePDFPageNumber] unsignedIntegerValue];
6769
}
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]) {
7771
prefersBitmap = YES;
7872
NSValue *sizeValue = options[SDImageCoderDecodeThumbnailPixelSize];
7973
#if SD_MAC
8074
imageSize = sizeValue.sizeValue;
8175
#else
8276
imageSize = sizeValue.CGSizeValue;
8377
#endif
84-
} else if (context[SDWebImageContextPDFPrefersBitmap]) {
85-
prefersBitmap = [context[SDWebImageContextPDFPrefersBitmap] boolValue];
8678
}
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];
9181
}
9282
#pragma clang diagnostic pop
9383

9484
UIImage *image;
9585
if (!prefersBitmap && [self.class supportsVectorPDFImage]) {
9686
image = [self createVectorPDFWithData:data pageNumber:pageNumber];
9787
} 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];
9990
}
10091

10192
image.sd_imageFormat = SDImageFormatPDF;
@@ -184,67 +175,6 @@ - (UIImage *)createVectorPDFWithData:(nonnull NSData *)data pageNumber:(NSUInteg
184175
return image;
185176
}
186177

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-
248178
+ (BOOL)supportsVectorPDFImage {
249179
#if SD_MAC
250180
// macOS's `NSImage` supports PDF built-in rendering

SDWebImagePDFCoder/Classes/SDWebImagePDFCoderDefine.h

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,6 @@
1313

1414
NS_ASSUME_NONNULL_BEGIN
1515

16-
/**
17-
A unsigned interger raw value which specify the desired PDF image page number. Because PDF can contains mutiple pages. The page number index is started with 0. (NSNumber)
18-
If you don't provide this value, use 0 (the first page) instead.
19-
*/
20-
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextPDFPageNumber;
21-
22-
/**
23-
A BOOL value which specify whether we prefer the actual bitmap representation instead of vector representation for PDF image. This is because the UIImage on iOS 11+ (NSImgae on macOS) can use the vector image format, which support dynamic scale without losing any detail. However, for some image processing logic, user may need the actual bitmap representation to manage pixels. Also, for lower firmware on iOS, the `UIImage` does not support vector rendering, user may want to handle them using the same code. (NSNumber)
24-
If you don't provide this value, use NO for default value and prefer the vector format when possible.
25-
@note Deprecated, use `SDWebImageContextImageThumbnailPixelSize`. Pass CGSize.zero means the mediaBox size of SVG.
26-
*/
27-
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextPDFPrefersBitmap __attribute__((deprecated("Use the new context option (for WebCache category), or coder option (for SDImageCoder protocol) instead. Pass CGSize.zero means the mediaBox size of PDF", "SDWebImageContextImageThumbnailPixelSize")));
28-
29-
#pragma mark - Bitmap Representation Options
30-
/**
31-
A CGSize raw value which specify the desired PDF image size during image loading. Because vector image like PDF format, may not contains a fixed size, or you want to get a larger size bitmap representation UIImage. (NSValue)
32-
If you don't provide this value, use the PDF mediaBox size instead.
33-
@note For iOS/tvOS 11+, you don't need this option and it will be ignored. Because UIImage support built-in vector rendering and scaling for PDF. Changing imageView's contentMode and bounds instead.
34-
@note For macOS user. Changing imageViews' imageScaling and bounds instead.
35-
*/
36-
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextPDFImageSize __attribute__((deprecated("Use the new context option (for WebCache category), or coder option (for SDImageCoder protocol) instead", "SDWebImageContextImageThumbnailPixelSize")));
37-
38-
/**
39-
A BOOL value which specify the whether PDF image should keep aspect ratio during image loading. Because when you specify image size via `SDWebImageContextPDFImageSize`, we need to know whether to keep aspect ratio or not when image size is not equal to PDF mediaBox size. (NSNumber)
40-
If you don't provide this value, use YES for default value.
41-
@note For iOS/tvOS 11+, you don't need this option and it will be ignored. Because UIImage support built-in vector rendering and scaling for PDF. Changing imageView's contentMode and bounds instead.
42-
@note For macOS user. Changing imageViews' imageScaling and bounds instead.
43-
*/
44-
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextPDFImagePreserveAspectRatio __attribute__((deprecated("Use the new context option (for WebCache category), or coder option (for SDImageCoder protocol) instead", "SDWebImageContextImagePreserveAspectRatio")));
45-
4616
#pragma mark - Coder Options
4717
/**
4818
A unsigned interger raw value which specify the desired PDF image page number. Because PDF can contains mutiple pages. The page number index is started with 0. (NSNumber)
@@ -52,8 +22,8 @@ FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextPDFIma
5222
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderDecodePDFPageNumber;
5323

5424
/**
55-
`SDImageCoderDecodeThumnailPixelSize`: The same as context option `SDWebImageContextImageThumbnailPixelSize`
56-
`SDImageCoderDecodePreserveAspectRatio`: The same as context option `SDWebImageContextImagePreserveAspectRatio`
25+
`SDImageCoderDecodeThumnailPixelSize`: See more in SDWebImage. Pass `.zero` means Bitmap PDF of MediaBox size, pass nil to prefer vector image. Defaults to nil.
26+
`SDImageCoderDecodePreserveAspectRatio`: See more in SDWebImage. Defaults to YES.
5727
*/
5828

5929
NS_ASSUME_NONNULL_END

SDWebImagePDFCoder/Classes/SDWebImagePDFCoderDefine.m

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,4 @@
77

88
#import "SDWebImagePDFCoderDefine.h"
99

10-
SDWebImageContextOption _Nonnull const SDWebImageContextPDFPageNumber = @"pdfPageNumber";
11-
SDWebImageContextOption _Nonnull const SDWebImageContextPDFPrefersBitmap = @"pdfPrefersBitmap";
12-
SDWebImageContextOption _Nonnull const SDWebImageContextPDFImageSize = @"pdfImageSize";
13-
SDWebImageContextOption _Nonnull const SDWebImageContextPDFImagePreserveAspectRatio = @"pdfImagePreserveAspectRatio";
14-
1510
SDImageCoderOption _Nonnull const SDImageCoderDecodePDFPageNumber = @"decodePDFPageNumber";

0 commit comments

Comments
 (0)