/Swift-UIImage-PDF

Simple UIImage PDF renderer category for iOS scalable assets in Swift Projects

Primary LanguageSwift

UIImage+PDF Swift

UIImage+PDF provides a UIImage class category method to render a UIImage from any PDF stored in the application bundle. The motivation for this was to enable the easy use of scaleable vector assets in Swift iOS apps.

#Usage Add the sources files in the UIImage+PDF sub folder to your project.

UIPDFView

Use UIPDFView for animation

UIPDFView(name: String, frame: CGRect)

Sample: 
let pdfView = UIPDFView(name: "animalsCat", frame: CGRectMake(10, 20, 100, 100))
        pdfView.backgroundColor = UIColor.clearColor()
        view.addSubview(pdfView)

UIImage

PDF asset name used without extension

Sample:

if let pdfImage: UIImage = UIImage.imageWithPDFNamed("animalsCat", fitSize: CGSizeMake(100, 100)){
            
            let pdfImageView: UIImageView = UIImageView(image: pdfImage)
            pdfImageView.center = CGPointMake(200, 200)
            
            view.addSubview(pdfImageView)
 }

PDF Asset with size to fit

UIImage.imageWithPDFNamed(name: String, fitSize: CGSize)

PDF Asset with Size

UIImage.imageWithPDFNamed(name: String, size: CGSize)

PDF Asset with Width

UIImage.imageWithPDFNamed(name: String, width: CGFloat)

PDF Asset with Height

UIImage.imageWithPDFNamed(name: String, height: CGFloat)

Disk Cacheing

Cached PDF assets to PNG images with readable names and scale factor ready to reuse.
UIImage+PDF now transparently caches all rendered PDFs in Application_Home/Documents/CachedAssets. This substantially improves application latency with large PDFs, especially on larger devices. To disable disk cacheing, call:

UIImage.shouldCacheOnDisk = false

Memory Cacheing

UIImage+PDF can now use NSCache to cache rendered PDFs in memory. This feature is disabled by default. To enable it call:

UIImage.shouldCacheInMemory = true

PDF file size

By default Adobe Illustrator saves exported PDFs very inefficiently. For best results, select File -> Save a Copy, select PDF format and then uncheck all the general options. Once you are ready to ship your app, run all your PDF assets through ShrinkIt (see below).

Other vector graphics editors which natively use the OSX Quartz renderer, such as Sketch, will create much more compact PDFs.

The amazing devs at Panic have now released a PDF shrinking utility, ShrinkIt, which should take a lot of the pain out of a vector asset workflow.

#Licence Copyright 2016 Roman Bambura - @RomanBambura. All rights reserved.

Permission is given to use this source code file without charge in any project, commercial or otherwise, entirely at your risk, with the condition that any redistribution (in part or whole) of source code must retain this copyright and permission notice. Attribution in compiled projects is appreciated but not required.

#Further Reading Matt Gemmell has an excellent article on his blog explaining how to use PDF images in iOS apps.

#UIImage+PDF Objective-C Nigel Timothy Barber UIImage-PDF