|
1 | 1 | # SDWebImagePDFCoder |
2 | 2 |
|
3 | | -[](https://travis-ci.org/lizhuoli1126@126.com/SDWebImagePDFCoder) |
4 | | -[](https://cocoapods.org/pods/SDWebImagePDFCoder) |
5 | | -[](https://cocoapods.org/pods/SDWebImagePDFCoder) |
6 | | -[](https://cocoapods.org/pods/SDWebImagePDFCoder) |
| 3 | +[](https://travis-ci.org/SDWebImage/SDWebImagePDFCoder) |
| 4 | +[](https://cocoapods.org/pods/SDWebImagePDFCoder) |
| 5 | +[](https://cocoapods.org/pods/SDWebImagePDFCoder) |
| 6 | +[](https://cocoapods.org/pods/SDWebImagePDFCoder) |
| 7 | +[](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). |
7 | 11 |
|
8 | 12 | ## Example |
9 | 13 |
|
10 | 14 | To run the example project, clone the repo, and run `pod install` from the Example directory first. |
11 | 15 |
|
| 16 | +You can modify the code or use some other PDF files to check the compatibility. |
| 17 | + |
12 | 18 | ## Requirements |
13 | 19 |
|
| 20 | ++ iOS 8 |
| 21 | ++ tvOS 9 |
| 22 | ++ macOS 10.10 |
| 23 | + |
14 | 24 | ## Installation |
15 | 25 |
|
| 26 | +#### CocoaPods |
| 27 | + |
16 | 28 | SDWebImagePDFCoder is available through [CocoaPods](https://cocoapods.org). To install |
17 | 29 | it, simply add the following line to your Podfile: |
18 | 30 |
|
19 | 31 | ```ruby |
20 | 32 | pod 'SDWebImagePDFCoder' |
21 | 33 | ``` |
22 | 34 |
|
| 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 | + |
23 | 103 | ## Author |
24 | 104 |
|
25 | | -lizhuoli1126@126.com, lizhuoli1126@126.com |
| 105 | +DreamPiggy |
26 | 106 |
|
27 | 107 | ## License |
28 | 108 |
|
29 | 109 | SDWebImagePDFCoder is available under the MIT license. See the LICENSE file for more info. |
| 110 | + |
| 111 | + |
0 commit comments