Skip to content

Instantly share code, notes, and snippets.

@SongJiaqiang
Created May 8, 2017 10:41
Show Gist options
  • Save SongJiaqiang/63e5ea66571316d711422393daf7d49f to your computer and use it in GitHub Desktop.
Save SongJiaqiang/63e5ea66571316d711422393daf7d49f to your computer and use it in GitHub Desktop.
draw PDF to image
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