Skip to content

Commit 0081fa2

Browse files
committed
Initial commit, support PDF vector rendering on iOS 11+/ bitmap rendering for iOS 10-
1 parent 10ccc3d commit 0081fa2

File tree

18 files changed

+470
-17
lines changed

18 files changed

+470
-17
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ Carthage/Build
3434
# Note: if you ignore the Pods directory, make sure to uncomment
3535
# `pod install` in .travis.yml
3636
#
37-
# Pods/
37+
Pods/

Example/Podfile.lock

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
PODS:
2+
- SDWebImage (5.0.0-beta3):
3+
- SDWebImage/Core (= 5.0.0-beta3)
4+
- SDWebImage/Core (5.0.0-beta3)
5+
- SDWebImagePDFCoder (0.1.0):
6+
- SDWebImage (>= 5.0.0-beta)
7+
8+
DEPENDENCIES:
9+
- SDWebImagePDFCoder (from `../`)
10+
11+
SPEC REPOS:
12+
https://github.com/cocoapods/specs.git:
13+
- SDWebImage
14+
15+
EXTERNAL SOURCES:
16+
SDWebImagePDFCoder:
17+
:path: "../"
18+
19+
SPEC CHECKSUMS:
20+
SDWebImage: e52654ceef9fdc19f4c612d64a5b6d3f05dd81a4
21+
SDWebImagePDFCoder: 28c0027dc67eb64633ae824166cc0af35ee6fb22
22+
23+
PODFILE CHECKSUM: ca5230ad7b6978adc4f1bdf02b8a0ba3005b984b
24+
25+
COCOAPODS: 1.5.3

Example/SDWebImagePDFCoder.xcodeproj/project.pbxproj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
071738DB83E780E2999FD6BA /* Pods-SDWebImagePDFCoder_Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SDWebImagePDFCoder_Tests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-SDWebImagePDFCoder_Tests/Pods-SDWebImagePDFCoder_Tests.debug.xcconfig"; sourceTree = "<group>"; };
4141
1745C32CD7A3DC952B084514 /* Pods-SDWebImagePDFCoder_Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SDWebImagePDFCoder_Tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-SDWebImagePDFCoder_Tests/Pods-SDWebImagePDFCoder_Tests.release.xcconfig"; sourceTree = "<group>"; };
4242
23D236E5C8333D1EF1AF6ED1 /* Pods-SDWebImagePDFCoder_Example.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SDWebImagePDFCoder_Example.release.xcconfig"; path = "Pods/Target Support Files/Pods-SDWebImagePDFCoder_Example/Pods-SDWebImagePDFCoder_Example.release.xcconfig"; sourceTree = "<group>"; };
43-
243AAAFCC482EEC1752548DA /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
43+
243AAAFCC482EEC1752548DA /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
4444
6003F58A195388D20070C39A /* SDWebImagePDFCoder_Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SDWebImagePDFCoder_Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
4545
6003F58D195388D20070C39A /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
4646
6003F58F195388D20070C39A /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
@@ -62,11 +62,11 @@
6262
606FC2411953D9B200FFA9A0 /* Tests-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Tests-Prefix.pch"; sourceTree = "<group>"; };
6363
644F99D8DED2C8FC92668CEC /* Pods_SDWebImagePDFCoder_Example.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SDWebImagePDFCoder_Example.framework; sourceTree = BUILT_PRODUCTS_DIR; };
6464
71719F9E1E33DC2100824A3D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
65-
7E8B2554E3F514245ECA9329 /* SDWebImagePDFCoder.podspec */ = {isa = PBXFileReference; includeInIndex = 1; name = SDWebImagePDFCoder.podspec; path = ../SDWebImagePDFCoder.podspec; sourceTree = "<group>"; };
65+
7E8B2554E3F514245ECA9329 /* SDWebImagePDFCoder.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = SDWebImagePDFCoder.podspec; path = ../SDWebImagePDFCoder.podspec; sourceTree = "<group>"; };
6666
86B75DB74FF533D71BDDF3F4 /* Pods_SDWebImagePDFCoder_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SDWebImagePDFCoder_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
6767
873B8AEA1B1F5CCA007FD442 /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = Main.storyboard; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
6868
B804462885514B9A9BD2089E /* Pods-SDWebImagePDFCoder_Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SDWebImagePDFCoder_Example.debug.xcconfig"; path = "Pods/Target Support Files/Pods-SDWebImagePDFCoder_Example/Pods-SDWebImagePDFCoder_Example.debug.xcconfig"; sourceTree = "<group>"; };
69-
DAF3465F3CB05A575EB1B674 /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; name = README.md; path = ../README.md; sourceTree = "<group>"; };
69+
DAF3465F3CB05A575EB1B674 /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
7070
/* End PBXFileReference section */
7171

7272
/* Begin PBXFrameworksBuildPhase section */
@@ -303,12 +303,14 @@
303303
);
304304
inputPaths = (
305305
"${SRCROOT}/Pods/Target Support Files/Pods-SDWebImagePDFCoder_Example/Pods-SDWebImagePDFCoder_Example-frameworks.sh",
306+
"${BUILT_PRODUCTS_DIR}/SDWebImage/SDWebImage.framework",
306307
"${BUILT_PRODUCTS_DIR}/SDWebImagePDFCoder/SDWebImagePDFCoder.framework",
307308
);
308309
name = "[CP] Embed Pods Frameworks";
309310
outputFileListPaths = (
310311
);
311312
outputPaths = (
313+
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SDWebImage.framework",
312314
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SDWebImagePDFCoder.framework",
313315
);
314316
runOnlyForDeploymentPostprocessing = 0;

Example/SDWebImagePDFCoder.xcworkspace/contents.xcworkspacedata

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
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>BuildSystemType</key>
6+
<string>Original</string>
7+
</dict>
8+
</plist>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info" : {
3+
"version" : 1,
4+
"author" : "xcode"
5+
}
6+
}

Example/SDWebImagePDFCoder/SDViewController.m

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//
88

99
#import "SDViewController.h"
10+
#import <SDWebImage/SDWebImage.h>
11+
#import <SDWebImagePDFCoder/SDWebImagePDFCoder.h>
1012

1113
@interface SDViewController ()
1214

@@ -18,6 +20,42 @@ - (void)viewDidLoad
1820
{
1921
[super viewDidLoad];
2022
// Do any additional setup after loading the view, typically from a nib.
23+
24+
SDImagePDFCoder *PDFCoder = [SDImagePDFCoder sharedCoder];
25+
[[SDImageCodersManager sharedManager] addCoder:PDFCoder];
26+
NSURL *pdfURL = [NSURL URLWithString:@"https://raw.githubusercontent.com/icons8/flat-color-icons/master/pdf/about.pdf"];
27+
NSURL *pdfURL2 = [NSURL URLWithString:@"https://raw.githubusercontent.com/icons8/flat-color-icons/master/pdf/webcam.pdf"];
28+
29+
CGSize screenSize = [UIScreen mainScreen].bounds.size;
30+
31+
UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, screenSize.height / 2)];
32+
imageView1.contentMode = UIViewContentModeScaleAspectFill;
33+
imageView1.clipsToBounds = YES;
34+
35+
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, screenSize.height / 2, screenSize.width, screenSize.height / 2)];
36+
imageView2.contentMode = UIViewContentModeScaleAspectFit;
37+
imageView2.clipsToBounds = YES;
38+
39+
[self.view addSubview:imageView1];
40+
[self.view addSubview:imageView2];
41+
42+
[imageView1 sd_setImageWithURL:pdfURL placeholderImage:nil options:SDWebImageRetryFailed completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
43+
if (image) {
44+
NSLog(@"PDF load success");
45+
}
46+
}];
47+
[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) {
48+
if (image) {
49+
NSLog(@"PDF load animation success");
50+
[UIView animateWithDuration:2 animations:^{
51+
imageView2.bounds = CGRectMake(0, 0, 2 * screenSize.width, screenSize.height);
52+
} completion:^(BOOL finished) {
53+
[UIView animateWithDuration:2 animations:^{
54+
imageView2.bounds = CGRectMake(0, 0, screenSize.width, screenSize.height / 2);
55+
}];
56+
}];
57+
}
58+
}];
2159
}
2260

2361
- (void)didReceiveMemoryWarning

Example/SDWebImagePDFCoder/SDWebImagePDFCoder-Info.plist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
<string>1.0</string>
2525
<key>LSRequiresIPhoneOS</key>
2626
<true/>
27+
<key>NSAppTransportSecurity</key>
28+
<dict>
29+
<key>NSAllowsArbitraryLoads</key>
30+
<true/>
31+
</dict>
2732
<key>UILaunchStoryboardName</key>
2833
<string>LaunchScreen</string>
2934
<key>UIMainStoryboardFile</key>

Example/Screenshot/PDFDemo.png

58.3 KB
Loading

README.md

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,111 @@
11
# SDWebImagePDFCoder
22

3-
[![CI Status](https://img.shields.io/travis/lizhuoli1126@126.com/SDWebImagePDFCoder.svg?style=flat)](https://travis-ci.org/lizhuoli1126@126.com/SDWebImagePDFCoder)
4-
[![Version](https://img.shields.io/cocoapods/v/SDWebImagePDFCoder.svg?style=flat)](https://cocoapods.org/pods/SDWebImagePDFCoder)
5-
[![License](https://img.shields.io/cocoapods/l/SDWebImagePDFCoder.svg?style=flat)](https://cocoapods.org/pods/SDWebImagePDFCoder)
6-
[![Platform](https://img.shields.io/cocoapods/p/SDWebImagePDFCoder.svg?style=flat)](https://cocoapods.org/pods/SDWebImagePDFCoder)
3+
[![CI Status](https://img.shields.io/travis/SDWebImage/SDWebImagePDFCoder.PDF?style=flat)](https://travis-ci.org/SDWebImage/SDWebImagePDFCoder)
4+
[![Version](https://img.shields.io/cocoapods/v/SDWebImagePDFCoder.PDF?style=flat)](https://cocoapods.org/pods/SDWebImagePDFCoder)
5+
[![License](https://img.shields.io/cocoapods/l/SDWebImagePDFCoder.PDF?style=flat)](https://cocoapods.org/pods/SDWebImagePDFCoder)
6+
[![Platform](https://img.shields.io/cocoapods/p/SDWebImagePDFCoder.PDF?style=flat)](https://cocoapods.org/pods/SDWebImagePDFCoder)
7+
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.PDF?style=flat)](https://github.com/SDWebImage/SDWebImagePDFCoder)
8+
9+
## What's for
10+
SDWebImagePDFCoder is a PDF coder plugin for [SDWebImage](https://github.com/rs/SDWebImage/) framework, which provide the image loading support for [PDF](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics). The PDF rendering is done using Apple's built-in framework (UIKit/AppKit/Core Graphics).
711

812
## Example
913

1014
To run the example project, clone the repo, and run `pod install` from the Example directory first.
1115

16+
You can modify the code or use some other PDF files to check the compatibility.
17+
1218
## Requirements
1319

20+
+ iOS 8
21+
+ tvOS 9
22+
+ macOS 10.10
23+
1424
## Installation
1525

26+
#### CocoaPods
27+
1628
SDWebImagePDFCoder is available through [CocoaPods](https://cocoapods.org). To install
1729
it, simply add the following line to your Podfile:
1830

1931
```ruby
2032
pod 'SDWebImagePDFCoder'
2133
```
2234

35+
#### Carthage
36+
37+
SDWebImagePDFCoder is available through [Carthage](https://github.com/Carthage/Carthage).
38+
39+
Note that because the dependency SDWebImage currently is in beta. You should use `Carthage v0.30.1` or above to support beta [sem-version](https://semver.org/).
40+
41+
```
42+
github "SDWebImage/SDWebImagePDFCoder"
43+
```
44+
45+
## Usage
46+
47+
To use PDF coder, you should firstly add the `SDImagePDFCoder` to the coders manager. Then you can call the View Category method to start load PDF images.
48+
49+
### Use UIImageView vector rendering (iOS/tvOS 11+, Mac)
50+
51+
**Important**: Apple add the built-in vector image support for PDF format for UIKit from iOS/tvOS 11+. Which means you can create a `UIImage` with PDF data, and set it on the `UIImageView`. When the imageView bounds/contentMode changed, the PDF image also get scaled without losing any detail. You can also use `+[UIImage imageNamed:]` with Xcode Asset Catalog for PDF image, remember to turn on `Preserve Vector Data`.
52+
53+
For macOS user, `NSImage`/`NSImageView` support PDF image from the day one. Use it as usual.
54+
55+
+ Objective-C
56+
57+
```objectivec
58+
SDImagePDFCoder *PDFCoder = [SDImagePDFCoder sharedCoder];
59+
[[SDImageCodersManager sharedManager] addCoder:PDFCoder];
60+
UIImageView *imageView;
61+
[imageView sd_setImageWithURL:url];
62+
```
63+
64+
+ Swift
65+
66+
```swift
67+
let PDFCoder = SDImagePDFCoder.shared
68+
SDImageCodersManager.shared.addCoder(PDFCoder)
69+
let imageView: UIImageView
70+
imageView.sd_setImage(with: url)
71+
```
72+
73+
### Use UIImageView bitmap rendering (iOS/tvOS 10-)
74+
75+
For firmware which is below iOS/tvOS 11+, `UIImage` && `UIImageView` does not support vector image rendering. Even you can add PDF image in Xcode Asset Catalog, it was encoded to bitmap PNG format when compiled but not support runtime scale.
76+
77+
For `UIImageView`, we will only parse PDF with a fixed image size (from the PDF cropBox information). But we also support you to specify a desired size during image loading using `PDFImageSize` context option. And you can specify whether to keep aspect ratio using `PDFImagePreserveAspectRatio` context option.
78+
79+
+ Objective-C
80+
81+
```objectivec
82+
SDImagePDFCoder *PDFCoder = [SDImagePDFCoder sharedCoder];
83+
[[SDImageCodersManager sharedManager] addCoder:PDFCoder];
84+
UIImageView *imageView;
85+
CGSize PDFImageSize = CGSizeMake(500, 500);
86+
[imageView sd_setImageWithURL:url placeholderImage:nil options:0 context:@{SDWebImageContextPDFImageSize : @(PDFImageSize)];
87+
```
88+
89+
+ Swift
90+
91+
```swift
92+
let PDFCoder = SDImagePDFCoder.shared
93+
SDImageCodersManager.shared.addCoder(PDFCoder)
94+
let imageView: UIImageView
95+
let PDFImageSize = CGSize(width: 500, height: 500)
96+
imageView.sd_setImage(with: url, placeholderImage: nil, options: [], context: [.pdfImageSize : PDFImageSize])
97+
```
98+
99+
## Screenshot
100+
101+
<img src="https://raw.githubusercontent.com/SDWebImage/SDWebImagePDFCoder/master/Example/Screenshot/PDFDemo.png" width="300" />
102+
23103
## Author
24104

25-
lizhuoli1126@126.com, lizhuoli1126@126.com
105+
DreamPiggy
26106

27107
## License
28108

29109
SDWebImagePDFCoder is available under the MIT license. See the LICENSE file for more info.
110+
111+

0 commit comments

Comments
 (0)