我知道如何在 PDFKit 中实现这一点。阅读文档后,有一个功能可以选择某些页面。如果您将其添加到 collectionFlowView 中,这可能会解决您的问题。
func selection(from startPage: PDFPage, atCharacterIndex startCharacter: Int, to endPage: PDFPage, atCharacterIndex endCharacter: Int) -> PDFSelection?
但是,当我读到您主要有图像时,还有另一个功能可以根据 CGPoints 提取 pdf 的部分内容:
func selection(from startPage: PDFPage, at startPoint: CGPoint, to endPage: PDFPage, at endPoint: CGPoint) -> PDFSelection?
也看看这个:https://developer.apple.com/documentation/pdfkit/pdfview
因为如果您只想查看页面而不进行任何注释编辑等,这可能是您所需要的。
我还准备了一些代码来提取下面的一页。希望对您有所帮助。
import PDFKit
import UIKit
class PDFViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
guard let url = Bundle.main.url(forResource: "myPDF", withExtension: "pdf") else {fatalError("INVALID URL")}
let pdf = PDFDocument(url: url)
let page = pdf?.page(at: 10) // returns a PDFPage instance
// now you have one page extracted and you can play around with it.
}
}
编辑 1:
看看这个代码提取。我知道整个 PDF 都会被加载,但是这种方法可能更节省内存,因为 iOS 可能会在 PDFView 中更好地处理它:
func readBook() {
if let oldBookView = self.view.viewWithTag(3) {
oldBookView.removeFromSuperview()
// This removes the old book view when the user chooses a new book language
}
if #available(iOS 11.0, *) {
let pdfView: PDFView = PDFView()
let path = BookManager.getBookPath(bookLanguageCode: book.bookLanguageCode)
let url = URL(fileURLWithPath: path)
if let pdfDocument = PDFDocument(url: url) {
pdfView.displayMode = .singlePageContinuous
pdfView.autoScales = true
pdfView.document = pdfDocument
pdfView.tag = 3 // I assigned a tag to this view so that later on I can easily find and remove it when the user chooses a new book language
let lastReadPage = getLastReadPage()
if let page = pdfDocument.page(at: lastReadPage) {
pdfView.go(to: page)
// Subscribe to notifications so the last read page can be saved
// Must subscribe after displaying the last read page or else, the first page will be displayed instead
NotificationCenter.default.addObserver(self, selector: #selector(self.saveLastReadPage),name: .PDFViewPageChanged, object: nil)
}
}
self.containerView.addSubview(pdfView)
setConstraints(view: pdfView)
addTapGesture(view: pdfView)
}
编辑 2:这不是 OP 正在寻找的答案。这也将整个 pdf 加载到内存中。
读取 cmets