【发布时间】:2020-02-17 06:05:58
【问题描述】:
我正在我的Swift 应用程序中实现 pdf 预览,所以我决定使用第三方库来预览我在下面的库中使用的 PDF
所以首先我下载 url 并存储到文档目录,然后显示它,但下面没有预览的 pdf 是我的代码
func downloadFileFromURL(url: String) {
if let audioUrl = URL(string: url) {
let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let destinationUrl = documentsDirectoryURL.appendingPathComponent(audioUrl.lastPathComponent)
print(destinationUrl)
if FileManager.default.fileExists(atPath: destinationUrl.path) {
print("The file already exists at path")
print(destinationUrl)
let document = try! PDFDocument(filePath: destinationUrl.lastPathComponent, password: "")
self.collectionView.document = document
} else {
URLSession.shared.downloadTask(with: audioUrl, completionHandler: { (location, response, error) -> Void in
guard let location = location, error == nil else { return }
do {
try FileManager.default.moveItem(at: location, to: destinationUrl)
print(destinationUrl)
let document = try! PDFDocument(filePath: destinationUrl.lastPathComponent, password: "")
self.collectionView.document = document
print("File moved to documents folder")
} catch let error as NSError {
print(error.localizedDescription)
}
}).resume()
}
}
}
在viewDidLoad() 内部,我正在执行下面的代码
downloadFileFromURL(url: "http://housedocs.house.gov/edlabor/AAHCA-BillText-071409.pdf")
但仍然没有预览 pdf 有人可以告诉我使用 UXMPdf 预览 pdf 的正确方法
或者建议我最好的 pdfviewer for Swift 从中我可以从 URL 加载 pdf
【问题讨论】: