Skip to content

Upgrade dependency of SDWebImage to 5.14.1+ #10

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

Merged
merged 1 commit into from
Nov 11, 2022
Merged
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>PreviewsEnabled</key>
<false/>
</dict>
</plist>
4 changes: 2 additions & 2 deletions Example/SDWebImagePDFCoder/SDViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ - (void)viewDidLoad
[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) {
if (image) {
NSLog(@"PDF bitmap load success.");
NSData *svgData = [image sd_imageDataAsFormat:SDImageFormatPDF];
NSAssert(!svgData, @"SVG Data should not exist");
NSData *pdfData = [image sd_imageDataAsFormat:SDImageFormatPDF];
NSAssert(!pdfData, @"PDF Data should not exist");
}
}];
}
Expand Down
6 changes: 3 additions & 3 deletions SDWebImagePDFCoder.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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

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

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

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

s.dependency 'SDWebImage', '~> 5.10'
s.dependency 'SDWebImage', '>= 5.14.1'
end
96 changes: 13 additions & 83 deletions SDWebImagePDFCoder/Classes/SDImagePDFCoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

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

@interface SDImageIOCoder ()

// From SDWebImage 5.14.1
+ (UIImage *)createBitmapPDFWithData:(nonnull NSData *)data pageNumber:(NSUInteger)pageNumber targetSize:(CGSize)targetSize preserveAspectRatio:(BOOL)preserveAspectRatio;

@end

#if SD_UIKIT || SD_WATCH
static SEL SDImageWithCGPDFPageSEL = NULL;
static SEL SDCGPDFPageSEL = NULL;
Expand Down Expand Up @@ -56,46 +63,30 @@ - (UIImage *)decodedImageWithData:(NSData *)data options:(SDImageCoderOptions *)
CGSize imageSize = CGSizeZero;
BOOL preserveAspectRatio = YES;

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
// Parse args
SDWebImageContext *context = options[SDImageCoderWebImageContext];
if (context[SDWebImageContextPDFPageNumber]) {
pageNumber = [context[SDWebImageContextPDFPageNumber] unsignedIntegerValue];
} else if (options[SDImageCoderDecodePDFPageNumber]) {
if (options[SDImageCoderDecodePDFPageNumber]) {
pageNumber = [options[SDImageCoderDecodePDFPageNumber] unsignedIntegerValue];
}
if (context[SDWebImageContextPDFImageSize]) {
prefersBitmap = YES;
NSValue *sizeValue = context[SDWebImageContextPDFImageSize];
#if SD_MAC
imageSize = sizeValue.sizeValue;
#else
imageSize = sizeValue.CGSizeValue;
#endif
} else if (options[SDImageCoderDecodeThumbnailPixelSize]) {
if (options[SDImageCoderDecodeThumbnailPixelSize]) {
prefersBitmap = YES;
NSValue *sizeValue = options[SDImageCoderDecodeThumbnailPixelSize];
#if SD_MAC
imageSize = sizeValue.sizeValue;
#else
imageSize = sizeValue.CGSizeValue;
#endif
} else if (context[SDWebImageContextPDFPrefersBitmap]) {
prefersBitmap = [context[SDWebImageContextPDFPrefersBitmap] boolValue];
}
if (context[SDWebImageContextPDFImagePreserveAspectRatio]) {
preserveAspectRatio = [context[SDWebImageContextPDFImagePreserveAspectRatio] boolValue];
} else if (options[SDImageCoderDecodePreserveAspectRatio]) {
preserveAspectRatio = [context[SDImageCoderDecodePreserveAspectRatio] boolValue];
if (options[SDImageCoderDecodePreserveAspectRatio]) {
preserveAspectRatio = [options[SDImageCoderDecodePreserveAspectRatio] boolValue];
}
#pragma clang diagnostic pop

UIImage *image;
if (!prefersBitmap && [self.class supportsVectorPDFImage]) {
image = [self createVectorPDFWithData:data pageNumber:pageNumber];
} else {
image = [self createBitmapPDFWithData:data pageNumber:pageNumber targetSize:imageSize preserveAspectRatio:preserveAspectRatio];
NSAssert([SDImageIOCoder respondsToSelector:@selector(createBitmapPDFWithData:pageNumber:targetSize:preserveAspectRatio:)], @"SDWebImage from 5.14.1 should contains this API");
image = [SDImageIOCoder createBitmapPDFWithData:data pageNumber:pageNumber targetSize:imageSize preserveAspectRatio:preserveAspectRatio];
}

image.sd_imageFormat = SDImageFormatPDF;
Expand Down Expand Up @@ -184,67 +175,6 @@ - (UIImage *)createVectorPDFWithData:(nonnull NSData *)data pageNumber:(NSUInteg
return image;
}

#pragma mark - Bitmap PDF representation
- (UIImage *)createBitmapPDFWithData:(nonnull NSData *)data pageNumber:(NSUInteger)pageNumber targetSize:(CGSize)targetSize preserveAspectRatio:(BOOL)preserveAspectRatio {
NSParameterAssert(data);
UIImage *image;

CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
if (!provider) {
return nil;
}
CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(provider);
CGDataProviderRelease(provider);
if (!document) {
return nil;
}

// `CGPDFDocumentGetPage` page number is 1-indexed.
CGPDFPageRef page = CGPDFDocumentGetPage(document, pageNumber + 1);
if (!page) {
CGPDFDocumentRelease(document);
return nil;
}

CGPDFBox box = kCGPDFMediaBox;
CGRect rect = CGPDFPageGetBoxRect(page, box);
CGRect targetRect = rect;
if (!CGSizeEqualToSize(targetSize, CGSizeZero)) {
targetRect = CGRectMake(0, 0, targetSize.width, targetSize.height);
}

CGFloat xRatio = targetRect.size.width / rect.size.width;
CGFloat yRatio = targetRect.size.height / rect.size.height;
CGFloat xScale = preserveAspectRatio ? MIN(xRatio, yRatio) : xRatio;
CGFloat yScale = preserveAspectRatio ? MIN(xRatio, yRatio) : yRatio;

// CGPDFPageGetDrawingTransform will only scale down, but not scale up, so we need calculcate the actual scale again
CGRect drawRect = CGRectMake( 0, 0, targetRect.size.width / xScale, targetRect.size.height / yScale);
CGAffineTransform scaleTransform = CGAffineTransformMakeScale(xScale, yScale);
CGAffineTransform transform = CGPDFPageGetDrawingTransform(page, box, drawRect, 0, preserveAspectRatio);

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, targetRect.size.height);
CGContextScaleCTM(context, 1, -1);
#endif

CGContextConcatCTM(context, scaleTransform);
CGContextConcatCTM(context, transform);

CGContextDrawPDFPage(context, page);

image = SDGraphicsGetImageFromCurrentImageContext();
SDGraphicsEndImageContext();

CGPDFDocumentRelease(document);

return image;
}

+ (BOOL)supportsVectorPDFImage {
#if SD_MAC
// macOS's `NSImage` supports PDF built-in rendering
Expand Down
34 changes: 2 additions & 32 deletions SDWebImagePDFCoder/Classes/SDWebImagePDFCoderDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,6 @@

NS_ASSUME_NONNULL_BEGIN

/**
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)
If you don't provide this value, use 0 (the first page) instead.
*/
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextPDFPageNumber;

/**
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)
If you don't provide this value, use NO for default value and prefer the vector format when possible.
@note Deprecated, use `SDWebImageContextImageThumbnailPixelSize`. Pass CGSize.zero means the mediaBox size of SVG.
*/
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")));

#pragma mark - Bitmap Representation Options
/**
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)
If you don't provide this value, use the PDF mediaBox size instead.
@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.
@note For macOS user. Changing imageViews' imageScaling and bounds instead.
*/
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextPDFImageSize __attribute__((deprecated("Use the new context option (for WebCache category), or coder option (for SDImageCoder protocol) instead", "SDWebImageContextImageThumbnailPixelSize")));

/**
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)
If you don't provide this value, use YES for default value.
@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.
@note For macOS user. Changing imageViews' imageScaling and bounds instead.
*/
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextPDFImagePreserveAspectRatio __attribute__((deprecated("Use the new context option (for WebCache category), or coder option (for SDImageCoder protocol) instead", "SDWebImageContextImagePreserveAspectRatio")));

#pragma mark - Coder Options
/**
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)
Expand All @@ -52,8 +22,8 @@ FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextPDFIma
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderDecodePDFPageNumber;

/**
`SDImageCoderDecodeThumnailPixelSize`: The same as context option `SDWebImageContextImageThumbnailPixelSize`
`SDImageCoderDecodePreserveAspectRatio`: The same as context option `SDWebImageContextImagePreserveAspectRatio`
`SDImageCoderDecodeThumnailPixelSize`: See more in SDWebImage. Pass `.zero` means Bitmap PDF of MediaBox size, pass nil to prefer vector image. Defaults to nil.
`SDImageCoderDecodePreserveAspectRatio`: See more in SDWebImage. Defaults to YES.
*/

NS_ASSUME_NONNULL_END
5 changes: 0 additions & 5 deletions SDWebImagePDFCoder/Classes/SDWebImagePDFCoderDefine.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,4 @@

#import "SDWebImagePDFCoderDefine.h"

SDWebImageContextOption _Nonnull const SDWebImageContextPDFPageNumber = @"pdfPageNumber";
SDWebImageContextOption _Nonnull const SDWebImageContextPDFPrefersBitmap = @"pdfPrefersBitmap";
SDWebImageContextOption _Nonnull const SDWebImageContextPDFImageSize = @"pdfImageSize";
SDWebImageContextOption _Nonnull const SDWebImageContextPDFImagePreserveAspectRatio = @"pdfImagePreserveAspectRatio";

SDImageCoderOption _Nonnull const SDImageCoderDecodePDFPageNumber = @"decodePDFPageNumber";