Skip to content

Support to export the PDF data for iOS && macOS. iOS need iOS 11+ for keeping vector format. #1

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 2 commits into from
May 12, 2019
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
16 changes: 10 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
language: objective-c
osx_image: xcode9.4
osx_image: xcode10.2

addons:
homebrew:
packages:
- carthage
update: true

env:
global:
Expand All @@ -15,10 +21,8 @@ notifications:
before_install:
- env
- locale
- gem install cocoapods --no-rdoc --no-ri --no-document --quiet
- gem install xcpretty --no-rdoc --no-ri --no-document --quiet
- brew update
- brew upgrade carthage
- gem install cocoapods --no-document --quiet
- gem install xcpretty --no-document --quiet
- pod --version
- pod setup --silent > /dev/null
- pod repo update --silent
Expand All @@ -30,7 +34,7 @@ script:
- set -o pipefail

- echo Check if the library described by the podspec can be built
#- pod lib lint --allow-warnings
- pod lib lint --allow-warnings

- echo Build as dynamic frameworks
- carthage update --platform ios,tvos,macos --configuration DEBUG
Expand Down
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "SDWebImage/SDWebImage" "5.0.0"
github "SDWebImage/SDWebImage" "5.0.2"
8 changes: 4 additions & 4 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PODS:
- SDWebImage (5.0.0):
- SDWebImage/Core (= 5.0.0)
- SDWebImage/Core (5.0.0)
- SDWebImage (5.0.2):
- SDWebImage/Core (= 5.0.2)
- SDWebImage/Core (5.0.2)
- SDWebImagePDFCoder (0.2.0):
- SDWebImage (~> 5.0)

Expand All @@ -17,7 +17,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
SDWebImage: 5de80a0302de9e377e62f47d2fa1304efff0e55f
SDWebImage: 6764b5fa0f73c203728052955dbefa2bf1f33282
SDWebImagePDFCoder: e980f143f97ff0bf8366a4b74d0acb50f0c9d8b6

PODFILE CHECKSUM: 13cc95bf609e99946808ed65a198e32a645f3acd
Expand Down
2 changes: 2 additions & 0 deletions Example/SDWebImagePDFCoder-Example-macOS/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ - (void)viewDidLoad {
[imageView1 sd_setImageWithURL:pdfURL placeholderImage:nil options:SDWebImageRetryFailed completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
if (image) {
NSLog(@"PDF load success");
NSData *pdfData = [image sd_imageDataAsFormat:SDImageFormatPDF];
NSAssert(pdfData.length > 0, @"PDF Data export failed");
}
}];
[imageView2 sd_setImageWithURL:pdfURL2 placeholderImage:nil options:SDWebImageRetryFailed context:@{SDWebImageContextPDFImageSize : @(imageView2.bounds.size)} progress:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
Expand Down
2 changes: 2 additions & 0 deletions Example/SDWebImagePDFCoder/SDViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ - (void)viewDidLoad
[imageView1 sd_setImageWithURL:pdfURL placeholderImage:nil options:SDWebImageRetryFailed completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
if (image) {
NSLog(@"PDF load success");
NSData *pdfData = [image sd_imageDataAsFormat:SDImageFormatPDF];
NSAssert(pdfData.length > 0, @"PDF Data export failed");
}
}];
[imageView2 sd_setImageWithURL:pdfURL2 placeholderImage:nil options:SDWebImageRetryFailed context:@{SDWebImageContextPDFImageSize : @(imageView2.bounds.size)} progress:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
Expand Down
36 changes: 15 additions & 21 deletions SDWebImagePDFCoder.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
32CA9E9C2185BC3D00322AF3 /* SDWebImagePDFCoder_iOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SDWebImagePDFCoder_iOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
32CA9E9C2185BC3D00322AF3 /* SDWebImagePDFCoder.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SDWebImagePDFCoder.framework; sourceTree = BUILT_PRODUCTS_DIR; };
32CA9EA72185BC7400322AF3 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = Module/Info.plist; sourceTree = "<group>"; };
32CA9EAE2185BCEA00322AF3 /* SDWebImagePDFCoder_tvOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SDWebImagePDFCoder_tvOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
32CA9EBB2185BD0700322AF3 /* SDWebImagePDFCoder_macOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SDWebImagePDFCoder_macOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
32CA9EAE2185BCEA00322AF3 /* SDWebImagePDFCoder.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SDWebImagePDFCoder.framework; sourceTree = BUILT_PRODUCTS_DIR; };
32CA9EBB2185BD0700322AF3 /* SDWebImagePDFCoder.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SDWebImagePDFCoder.framework; sourceTree = BUILT_PRODUCTS_DIR; };
32CA9EC32185BDBB00322AF3 /* SDWebImagePDFCoder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDWebImagePDFCoder.h; path = Module/SDWebImagePDFCoder.h; sourceTree = "<group>"; };
32CA9EC82185BDC300322AF3 /* SDWebImagePDFCoderDefine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDWebImagePDFCoderDefine.h; sourceTree = "<group>"; };
32CA9ECA2185BDC300322AF3 /* SDImagePDFCoder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDImagePDFCoder.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -82,9 +82,9 @@
32CA9E9D2185BC3D00322AF3 /* Products */ = {
isa = PBXGroup;
children = (
32CA9E9C2185BC3D00322AF3 /* SDWebImagePDFCoder_iOS.framework */,
32CA9EAE2185BCEA00322AF3 /* SDWebImagePDFCoder_tvOS.framework */,
32CA9EBB2185BD0700322AF3 /* SDWebImagePDFCoder_macOS.framework */,
32CA9E9C2185BC3D00322AF3 /* SDWebImagePDFCoder.framework */,
32CA9EAE2185BCEA00322AF3 /* SDWebImagePDFCoder.framework */,
32CA9EBB2185BD0700322AF3 /* SDWebImagePDFCoder.framework */,
);
name = Products;
sourceTree = "<group>";
Expand Down Expand Up @@ -171,7 +171,7 @@
);
name = "SDWebImagePDFCoder iOS";
productName = SDWebImagePDFCoder;
productReference = 32CA9E9C2185BC3D00322AF3 /* SDWebImagePDFCoder_iOS.framework */;
productReference = 32CA9E9C2185BC3D00322AF3 /* SDWebImagePDFCoder.framework */;
productType = "com.apple.product-type.framework";
};
32CA9EAD2185BCEA00322AF3 /* SDWebImagePDFCoder tvOS */ = {
Expand All @@ -189,7 +189,7 @@
);
name = "SDWebImagePDFCoder tvOS";
productName = "SDWebImagePDFCoder-tvOS";
productReference = 32CA9EAE2185BCEA00322AF3 /* SDWebImagePDFCoder_tvOS.framework */;
productReference = 32CA9EAE2185BCEA00322AF3 /* SDWebImagePDFCoder.framework */;
productType = "com.apple.product-type.framework";
};
32CA9EBA2185BD0700322AF3 /* SDWebImagePDFCoder macOS */ = {
Expand All @@ -207,7 +207,7 @@
);
name = "SDWebImagePDFCoder macOS";
productName = "SDWebImagePDFCoder-macOS";
productReference = 32CA9EBB2185BD0700322AF3 /* SDWebImagePDFCoder_macOS.framework */;
productReference = 32CA9EBB2185BD0700322AF3 /* SDWebImagePDFCoder.framework */;
productType = "com.apple.product-type.framework";
};
/* End PBXNativeTarget section */
Expand Down Expand Up @@ -444,8 +444,7 @@
"@loader_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.SDWebImagePDFCoder-iOS";
PRODUCT_MODULE_NAME = SDWebImagePDFCoder;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
PRODUCT_NAME = SDWebImagePDFCoder;
SKIP_INSTALL = YES;
TARGETED_DEVICE_FAMILY = "1,2";
};
Expand Down Expand Up @@ -473,8 +472,7 @@
"@loader_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.SDWebImagePDFCoder-iOS";
PRODUCT_MODULE_NAME = SDWebImagePDFCoder;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
PRODUCT_NAME = SDWebImagePDFCoder;
SKIP_INSTALL = YES;
TARGETED_DEVICE_FAMILY = "1,2";
};
Expand All @@ -501,8 +499,7 @@
"@loader_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.SDWebImagePDFCoder-tvOS";
PRODUCT_MODULE_NAME = SDWebImagePDFCoder;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
PRODUCT_NAME = SDWebImagePDFCoder;
SDKROOT = appletvos;
SKIP_INSTALL = YES;
TARGETED_DEVICE_FAMILY = 3;
Expand Down Expand Up @@ -531,8 +528,7 @@
"@loader_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.SDWebImagePDFCoder-tvOS";
PRODUCT_MODULE_NAME = SDWebImagePDFCoder;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
PRODUCT_NAME = SDWebImagePDFCoder;
SDKROOT = appletvos;
SKIP_INSTALL = YES;
TARGETED_DEVICE_FAMILY = 3;
Expand Down Expand Up @@ -564,8 +560,7 @@
);
MACOSX_DEPLOYMENT_TARGET = 10.10;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.SDWebImagePDFCoder-macOS";
PRODUCT_MODULE_NAME = SDWebImagePDFCoder;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
PRODUCT_NAME = SDWebImagePDFCoder;
SDKROOT = macosx;
SKIP_INSTALL = YES;
};
Expand Down Expand Up @@ -595,8 +590,7 @@
);
MACOSX_DEPLOYMENT_TARGET = 10.10;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.SDWebImagePDFCoder-macOS";
PRODUCT_MODULE_NAME = SDWebImagePDFCoder;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
PRODUCT_NAME = SDWebImagePDFCoder;
SDKROOT = macosx;
SKIP_INSTALL = YES;
};
Expand Down
40 changes: 37 additions & 3 deletions SDWebImagePDFCoder/Classes/SDImagePDFCoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ - (instancetype)_initWithCGPDFPage:(CGPDFPageRef)page;
- (instancetype)_initWithCGPDFPage:(CGPDFPageRef)page scale:(double)scale orientation:(UIImageOrientation)orientation;
+ (instancetype)_imageWithCGPDFPage:(CGPDFPageRef)page;
+ (instancetype)_imageWithCGPDFPage:(CGPDFPageRef)page scale:(double)scale orientation:(UIImageOrientation)orientation;
- (CGPDFPageRef)_CGPDFPage;

@end
#endif
Expand Down Expand Up @@ -81,11 +82,44 @@ - (UIImage *)decodedImageWithData:(NSData *)data options:(SDImageCoderOptions *)


- (BOOL)canEncodeToFormat:(SDImageFormat)format {
return NO;
return format == SDImageFormatPDF;
}

- (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format options:(SDImageCoderOptions *)options {
return nil;
if (![self.class supportsVectorPDFImage]) {
return nil;
}
#if SD_MAC
// Pixel size use `NSImageRepMatchesDevice` to avoid CGImage bitmap format
NSRect imageRect = NSMakeRect(0, 0, NSImageRepMatchesDevice, NSImageRepMatchesDevice);
NSImageRep *imageRep = [image bestRepresentationForRect:imageRect context:nil hints:nil];
if (![imageRep isKindOfClass:NSPDFImageRep.class]) {
return nil;
}
return ((NSPDFImageRep *)imageRep).PDFRepresentation;
#else
CGPDFPageRef page = [image _CGPDFPage];
if (!page) {
return nil;
}

// Draw the PDF page using PDFContextToData
NSMutableData *data = [NSMutableData data];
CGPDFBox box = kCGPDFMediaBox;
CGRect rect = CGPDFPageGetBoxRect(page, box);

UIGraphicsBeginPDFContextToData(data, CGRectZero, nil);
UIGraphicsBeginPDFPageWithInfo(rect, nil);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
// Core Graphics Coordinate System convert
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, 0, -CGRectGetHeight(rect));
CGContextDrawPDFPage(context, page);
UIGraphicsEndPDFContext();

return [data copy];
#endif
}

#pragma mark - Vector PDF representation
Expand Down Expand Up @@ -149,7 +183,7 @@ - (UIImage *)createBitmapPDFWithData:(nonnull NSData *)data pageNumber:(NSUInteg
return nil;
}

CGPDFBox box = kCGPDFCropBox;
CGPDFBox box = kCGPDFMediaBox;
Copy link
Collaborator Author

@dreampiggy dreampiggy May 12, 2019

Choose a reason for hiding this comment

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

Actually, we should use mediabox for general usage. The PDF with cropbox may contains conflict issue when using on iOS with alignmentInsets. If user want, they can do post-processing using transformer. Or this may become a extra decoding options in the future.

CGRect rect = CGPDFPageGetBoxRect(page, box);
CGRect targetRect = rect;
if (!CGSizeEqualToSize(targetSize, CGSizeZero)) {
Expand Down