Created
July 9, 2017 22:18
-
-
Save KrisYu/492f1f8fc2586654dfaccb18af2740c5 to your computer and use it in GitHub Desktop.
save UIImage as pdf file
This file contains hidden or 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
| //: Playground - noun: a place where people can play | |
| import UIKit | |
| let a = #imageLiteral(resourceName: "hot.png") | |
| func createPDF(image: UIImage) -> NSData? { | |
| let pdfData = NSMutableData() | |
| let pdfConsumer = CGDataConsumer(data: pdfData as CFMutableData)! | |
| var mediaBox = CGRect.init(x: 0, y: 0, width: image.size.width, height: image.size.height) | |
| let pdfContext = CGContext(consumer: pdfConsumer, mediaBox: &mediaBox, nil)! | |
| pdfContext.beginPage(mediaBox: &mediaBox) | |
| pdfContext.draw(image.cgImage!, in: mediaBox) | |
| pdfContext.endPage() | |
| return pdfData | |
| } | |
| let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! | |
| let docURL = documentDirectory.appendingPathComponent("myFileName.pdf") | |
| print(docURL) | |
| try createPDF(image: a)?.write(to: docURL, atomically: true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment