Url让我获得pdf。我需要一个4页的pdf文件来获取UIImage,但不是全部,而只是页面的一部分。请告诉我如何使这更容易。感谢您花费的时间
答案 0 :(得分:4)
以下两个Swift 4.2 / iOS 12策略显示了如何从PDF文件页面获取UIImage
。从PDF文件页面中提取特定帧作为练习。
PDFDocument
和PDFPage
' draw(with:to:)
方法(需要iOS 11) PDFPage
有一个名为draw(with:to:)
的方法。 draw(with:to:)
有以下声明:
func draw(with box: PDFDisplayBox, to context: CGContext)
以下Playground代码显示了如何使用UIImage
PDFPage
方法从PDF文件创建draw(with:to:)
:
import UIKit
import PDFKit
import PlaygroundSupport
let url = Bundle.main.url(forResource: "Invoice", withExtension: "pdf")!
let document = PDFDocument(url: url)!
let page = document.page(at: 0)!
let rect = page.bounds(for: PDFDisplayBox.mediaBox).applying(page.transform(for: .mediaBox))
let renderer = UIGraphicsImageRenderer(size: rect.size)
let image = renderer.image(actions: { context in
let cgContext = context.cgContext
let rect = cgContext.boundingBoxOfClipPath
cgContext.setFillColor(gray: 1, alpha: 1)
cgContext.fill(rect)
cgContext.translateBy(x: 0, y: rect.size.height)
cgContext.scaleBy(x: 1, y: -1)
page.draw(with: PDFDisplayBox.mediaBox, to: cgContext)
})
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFit
PlaygroundPage.current.liveView = imageView
CGPDFDocument
和CGContext
' drawPDFPage(_:)
方法 CGContext
有一个名为drawPDFPage(_:)
的方法。 drawPDFPage(_:)
有以下声明:
func drawPDFPage(_ page: CGPDFPage)
将PDF页面的内容绘制到当前图形上下文中。
以下操场代码显示如何使用UIImage
CGContext
方法从PDF文件创建drawPDFPage(_:)
:
没有页面轮换管理的代码
import UIKit
import PlaygroundSupport
let url = Bundle.main.url(forResource: "Invoice", withExtension: "pdf")!
let document = CGPDFDocument(url as CFURL)!
let page = document.page(at: 1)!
let rect = page.getBoxRect(CGPDFBox.mediaBox)
let renderer = UIGraphicsImageRenderer(size: rect.size)
let image = renderer.image(actions: { context in
let cgContext = context.cgContext
cgContext.setFillColor(gray: 1, alpha: 1)
cgContext.fill(rect)
cgContext.translateBy(x: 0, y: rect.size.height)
cgContext.scaleBy(x: 1, y: -1)
cgContext.drawPDFPage(page)
})
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFit
PlaygroundPage.current.liveView = imageView
管理页面轮换的替代代码。注意:由于使用CGPDFPage
' getDrawingTransform(_:rect:rotate:preserveAspectRatio:)
,图片将按比例缩小
import UIKit
import PlaygroundSupport
let url = Bundle.main.url(forResource: "Invoice", withExtension: "pdf")!
let document = CGPDFDocument(url as CFURL)!
let page = document.page(at: 1)!
let rect = page.getBoxRect(CGPDFBox.mediaBox)
let drawSize = rect.applying(page.getDrawingTransform(.mediaBox, rect: rect, rotate: 0, preserveAspectRatio: true)).size
let renderer = UIGraphicsImageRenderer(size: drawSize)
let image = renderer.image(actions: { context in
let cgContext = context.cgContext
let rect = cgContext.boundingBoxOfClipPath
cgContext.setFillColor(gray: 1, alpha: 1)
cgContext.fill(rect)
cgContext.scaleBy(x: 1, y: -1)
cgContext.translateBy(x: 0, y: -rect.size.height)
cgContext.concatenate(page.getDrawingTransform(.mediaBox, rect: rect, rotate: 0, preserveAspectRatio: true))
cgContext.drawPDFPage(page)
})
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFit
PlaygroundPage.current.liveView = imageView
答案 1 :(得分:1)
使用此框架将UIImage转换为PDF。