Created
May 8, 2017 10:41
-
-
Save SongJiaqiang/63e5ea66571316d711422393daf7d49f to your computer and use it in GitHub Desktop.
draw PDF to image
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
func drawPDFfromURL(url: URL) -> UIImage? { | |
guard let document = CGPDFDocument(url as CFURL) else { return nil } | |
guard let page = document.page(at: 1) else { return nil } | |
let pageRect = page.getBoxRect(.mediaBox) | |
let renderer = UIGraphicsImageRenderer(size: pageRect.size) | |
let img = renderer.image { ctx in | |
UIColor.white.set() | |
ctx.fill(pageRect) | |
ctx.cgContext.translateBy(x: 0.0, y: pageRect.size.height); | |
ctx.cgContext.scaleBy(x: 1.0, y: -1.0); | |
ctx.cgContext.drawPDFPage(page); | |
} | |
return img | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment