Last active
November 17, 2016 09:16
Swift replacement for UIImage+PDF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PDFImage.swift. Copyright © 2016 Nigel Timothy Barber (@mindbrix). All rights reserved. | |
// | |
import UIKit | |
extension UIImage { | |
static func PDFImageWith(url: NSURL, pageNumber: Int, width: CGFloat) -> UIImage? { | |
return PDFImageWith(url, pageNumber: pageNumber, constraints: CGSizeMake(width, 0)) | |
} | |
static func PDFImageWith(url: NSURL, pageNumber: Int, height: CGFloat) -> UIImage? { | |
return PDFImageWith(url, pageNumber: pageNumber, constraints: CGSizeMake(0, height)) | |
} | |
static func PDFImageWith(url: NSURL, pageNumber: Int) -> UIImage? { | |
return PDFImageWith(url, pageNumber: pageNumber, constraints: CGSizeMake(0, 0)) | |
} | |
static func PDFImageWith(url: NSURL, pageNumber: Int, constraints: CGSize) -> UIImage? { | |
if let pdf = CGPDFDocumentCreateWithURL(url) { | |
if let page = CGPDFDocumentGetPage(pdf, pageNumber) { | |
let size = CGPDFPageGetBoxRect(page, .MediaBox).size.forConstraints(constraints) | |
let cacheURL = url.PDFImageCacheURL(pageNumber, size: size) | |
if let url = cacheURL { | |
if NSFileManager.defaultManager().fileExistsAtPath(url.path!) { | |
if let image = UIImage(contentsOfFile: url.path!) { | |
return UIImage(CGImage: image.CGImage!, scale: UIScreen.mainScreen().scale, orientation: .Up) | |
} | |
} | |
} | |
UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.mainScreen().scale) | |
if let ctx = UIGraphicsGetCurrentContext() { | |
CGContextConcatCTM(ctx, CGAffineTransformMake(1, 0, 0, -1, 0, size.height)) | |
CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(page, .MediaBox, CGRectMake(0, 0, size.width, size.height), 0, true)) | |
CGContextDrawPDFPage(ctx, page) | |
let image = UIGraphicsGetImageFromCurrentImageContext() | |
if let url = cacheURL { | |
if let imageData = UIImagePNGRepresentation(image) { | |
imageData.writeToFile(url.path!, atomically:false) | |
} | |
} | |
return UIImage(CGImage: image.CGImage!, scale: UIScreen.mainScreen().scale, orientation: .Up) | |
} | |
} | |
} | |
return nil | |
} | |
} | |
extension CGSize { | |
func forConstraints(constraints: CGSize) -> CGSize { | |
if constraints.width == 0 && constraints.height == 0 { | |
return self | |
} | |
let sx = constraints.width / width, sy = constraints.height / height | |
let s = sx != 0 && sy != 0 ? min(sx, sy) : max(sx, sy) | |
return CGSizeMake(ceil(width * s), ceil(height * s)) | |
} | |
} | |
extension NSURL { | |
func PDFImageCacheURL(pageNumber: Int, size: CGSize) -> NSURL? { | |
if let path = self.path { | |
do { | |
let attributes = try NSFileManager.defaultManager().attributesOfItemAtPath(path) | |
if let fileSize = attributes[NSFileSize] as! NSNumber? { | |
if let fileDate = attributes[NSFileModificationDate] as! NSDate? { | |
let hashables = path + fileSize.stringValue + String(fileDate.timeIntervalSince1970) + String(size) | |
let cacheDirectory = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)[0] + "/__PDF_CACHE__" | |
do { | |
try NSFileManager.defaultManager().createDirectoryAtPath(cacheDirectory, withIntermediateDirectories: true, attributes:nil) | |
} catch {} | |
return NSURL(fileURLWithPath: cacheDirectory + "/" + String(format:"%2X", hashables.hash) + ".png") | |
} | |
} | |
} catch {} | |
} | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment