Swiftpack.co - Package - SDWebImage/SDWebImagePDFCoder

SDWebImagePDFCoder

CI Status Version License Platform Carthage compatible SwiftPM 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+
  • watchOS 2+

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"

Swift Package Manager (Xcode 11+)

SDWebImagePDFCoder is available through Swift Package Manager.

let package = Package(
    dependencies: [
        .package(url: "https://github.com/SDWebImage/SDWebImagePDFCoder.git", from: "0.6")
    ]
)

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 mediaBox information). But we also support you to specify a desired size during image loading using .imageThumbnailPixelSize context option. And you can specify whether or not to keep aspect ratio during scale using .imagePreserveAspectRatio context option.

Note: Once you pass the pixel size, we will always generate the bitmap representation even on iOS/tvOS 11+. If you want the vector format, do not pass them, let UIImageView to dynamically stretch the PDF.

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

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. The bitmap form of PDF does not support PDF data export as well.

  • Objective-C
UIImage *pdfImage; // UIImage with vector image, or NSImage contains `NSPDFImageRep`
if (pdfImage.sd_isVector) { // This API available in SDWebImage 5.6.0
    NSData *pdfData = [pdfImage sd_imageDataAsFormat:SDImageFormatPDF];
}
  • Swift
let pdfImage: UIImage // UIImage with vector image, or NSImage contains `NSPDFImageRep`
if pdfImage.sd_isVector { // This API available in SDWebImage 5.6.0
    let pdfData = pdfImage.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.

Github

link
Stars: 9

Dependencies

Used By

Total: 0

Releases

0.6.0 - Support SDWebImage 5.5 - 2020-01-31 11:57:34

Features

  • Update to support SDWebImage 5.5's new thumbnailPixelSize option #6

Deprecation

Deprecated the current PDF prefixed options:

  • SDWebImageContextPDFPrefersBitmap
  • SDWebImageContextPDFImageSize
  • SDWebImageContextPDFImagePreserveAspectRatio

Fix CocoaPods Static Library issue - 2019-12-13 08:30:19

Fixes

  • Fix the compatible issue when using the Static Library (CocoaPods) to install SDWebImage, also keep the module import for framework target

Hidden Symbol and Catalyst - 2019-12-01 10:42:30

Project

  • Changing the PDF symbol naming with the encoded string instead #3
  • Add support for Catalyst #4

watchOS and SwiftPM! - 2019-11-11 12:18:39

Features

  • watchOS platform support #2
  • SwiftPM support #2

Supports PDF data export - 2019-05-12 11:22:17

Feature

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

Compatible for SDWebImage 5.0 - 2019-05-12 11:21:33

Project

  • Upgrade the SDWebImage dependency to ~> SDWebImage 5.0

Upstream dependency update && Fix Carthage - 2019-01-26 11:39:13

Fix

  • Upgrade the upstream dependency to 5.0.0-beta4. Fix the compile issue.
  • Fix the Carthage install module name. Which should be just SDWebImagePDFCoder without any suffix.

Initial Release - 2018-12-05 12:57:12

Feature

  • Supports built-in vector renderingfor PDF images, on iOS 11+/tvOS 11+/macOS 10.10+. All the built-in UIKit/AppKit class, support PDF vector render natively. Old firmware will fallback to bitmap representation.
  • Supports decoding PDF image into bitmap representation using Core Graphics framework
  • Supports specify desired PDF page number / bitmap size for single image request