Latest 0.3.0
Homepage https://github.com/SDWebImage/SDWebImagePDFCoder
License MIT
Platforms ios 8.0, tvos 9.0, osx 10.10
Dependencies SDWebImage
Authors

CI Status
Version
License
Platform
Carthage compatible

What’s for

SDWebImagePDFCoder is a PDF coder plugin for SDWebImage framework, which provide the image loading support for PDF. The PDF rendering is done using Apple’s built-in framework (UIKit/AppKit/Core Graphics).

Example

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

You can modify the code or use some other PDF files to check the compatibility.

Requirements

  • iOS 8
  • tvOS 9
  • macOS 10.10

Installation

CocoaPods

SDWebImagePDFCoder is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'SDWebImagePDFCoder'

Carthage

SDWebImagePDFCoder is available through Carthage.

github "SDWebImage/SDWebImagePDFCoder"

Usage

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.

Use UIImageView vector rendering (iOS/tvOS 11+, Mac)

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.

For macOS user, NSImage/NSImageView support PDF image from the day one. Use it as usual.

  • Objective-C
SDImagePDFCoder *PDFCoder = [SDImagePDFCoder sharedCoder];
[[SDImageCodersManager sharedManager] addCoder:PDFCoder];
UIImageView *imageView;
[imageView sd_setImageWithURL:url];
  • Swift
let PDFCoder = SDImagePDFCoder.shared
SDImageCodersManager.shared.addCoder(PDFCoder)
let imageView: UIImageView
imageView.sd_setImage(with: url)

Use UIImageView bitmap rendering (iOS/tvOS 10-)

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.

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 or not to keep aspect ratio during scale using PDFImagePreserveAspectRatio context option.

  • Objective-C
SDImagePDFCoder *PDFCoder = [SDImagePDFCoder sharedCoder];
[[SDImageCodersManager sharedManager] addCoder:PDFCoder];
UIImageView *imageView;
CGSize PDFImageSize = CGSizeMake(500, 500);
[imageView sd_setImageWithURL:url placeholderImage:nil options:0 context:@{SDWebImageContextPDFImageSize : @(PDFImageSize)];
  • Swift
let PDFCoder = SDImagePDFCoder.shared
SDImageCodersManager.shared.addCoder(PDFCoder)
let imageView: UIImageView
let PDFImageSize = CGSize(width: 500, height: 500)
imageView.sd_setImage(with: url, placeholderImage: nil, options: [], context: [.pdfImageSize : PDFImageSize])

Export PDF data

SDWebImagePDFCoder provide an easy way to export the PDF image generated from framework, to the original PDF data.

Note: For firmware which is below iOS/tvOS 11+, UIImage does not support PDF vector image as well as exporting.

  • Objective-C
UIImage *image; // UIImage with vector image, or NSImage contains `NSPDFImageRep`
NSData *imageData = [image sd_imageDataAsFormat:SDImageFormatPDF];
  • Swift
let image; // UIImage with vector image, or NSImage contains `NSPDFImageRep`
let imageData = image.sd_imageData(as: .PDF)

Screenshot


These PDF images are from icons8, you can try the demo with your own PDF image as well.

Author

DreamPiggy

License

SDWebImagePDFCoder is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "SDWebImagePDFCoder",
    "version": "0.3.0",
    "summary": "A PDF coder plugin for SDWebImage, using built-in framework",
    "description": "SDWebImageSVGCoder is a SVG coder plugin for SDWebImage framework, which provide the image loading support for SVG using SVGKit SVG engine.",
    "homepage": "https://github.com/SDWebImage/SDWebImagePDFCoder",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "DreamPiggy": "[email protected]"
    },
    "source": {
        "git": "https://github.com/SDWebImage/SDWebImagePDFCoder.git",
        "tag": "0.3.0"
    },
    "platforms": {
        "ios": "8.0",
        "tvos": "9.0",
        "osx": "10.10"
    },
    "source_files": [
        "SDWebImagePDFCoder/Classes/**/*",
        "SDWebImagePDFCoder/Module/SDWebImagePDFCoder.h"
    ],
    "module_map": "SDWebImagePDFCoder/Module/SDWebImagePDFCoder.modulemap",
    "dependencies": {
        "SDWebImage": [
            "~> 5.0"
        ]
    }
}

Pin It on Pinterest

Share This