PDFView不显示我的PDF [英] PDFView does not display my PDF
问题描述
我无法获取pdfView来显示URL中的PDF.我可以获取文件,并且根据控制台它确实存在.这是我所做的:
I am having trouble getting my pdfView to display a PDF from a URL. I can get the file, and it definitely exists according to the console. Here's what I've done:
首先,我声明一个文档选择器:
First, I declare a document picker:
@IBAction func addFromICloud(_ sender: UIBarButtonItem) {
let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.composite-content"], in: .import)
documentPicker.delegate = self
documentPicker.modalPresentationStyle = UIModalPresentationStyle.formSheet
present(documentPicker, animated: true, completion: nil)
}
第二,我从DocumentPickerViewController获取URL和文档名称:
Second, I get a URL and the name of the document from DocumentPickerViewController:
func documentPicker(_ controller: UIDocumentPickerViewController,
didPickDocumentAt url: URL) {
print ("Picker's url is \(url)")
theName = String(describing:url.lastPathComponent)
books.append(theName)
tableView.reloadData()
}
然后,我将TheName附加到文档目录路径:
Then, I append theName to the document directory path:
fileIsFromService = true
indexPoint = indexPath.row
let DocumentDirectory = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
docURL = DocumentDirectory.appendingPathComponent(theName)
然后,我确保该文件存在,并且确实存在:
Then I make sure that the file exists, and it does:
let fileExists = FileManager().fileExists(atPath: (finalDocURL?.path)!)
print ("The value of fileExists is \(fileExists)")
addressArray.append(docURL!)
print ("The url to be passed should be \(docURL!)")
print ("addressArray is now \(addressArray)")
indexPoint = indexPath.row
viewController.load(books[indexPath.row])
然后,在viewController中,我尝试使用docURL加载pdfView:
Then, in viewController, I try to load the pdfView with docURL:
func load(_ name: String) {
if fileIsFromService == true {
guard let path = docURL else {return}
if let document = PDFDocument(url: path){
pdfView.document = document
}
pdfView.goToFirstPage(nil)
if UIDevice.current.userInterfaceIdiom == .pad {
title = name
}
}
我没有任何错误,只是空白的pdfView.我知道已经添加了pdfView的子视图.没有变量为零.我怀疑我需要从docURL中获取数据,但是我应该在哪里写呢?
I get no errors, just a blank pdfView. I know that the pdfView's subview has been added. No variables are nil. I suspect that I need to get the data out of docURL, but where would I write it?
推荐答案
问题是您要导入的pdf只是一个临时文件.您需要将临时文件url复制/移动到您的应用程序文档文件夹中.
The problem is that the pdf you are importing it is just a temporary file. You need to copy/move the temporary file url to your app documents folder.
UIDocumentPickerModeImport URL指向所选对象的副本 文档.该文档是一个临时文件.它仍然仅可用 直到您的应用程序终止.要保留永久副本,您必须 将此文件移到沙箱中的永久位置.
UIDocumentPickerModeImport The URL refers to a copy of the selected document. This document is a temporary file. It remains available only until your application terminates. To keep a permanent copy, you must move this file to a permanent location inside your sandbox.
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
do {
try FileManager.default.moveItem(at: url.standardizedFileURL, to: documentDirectory.appendingPathComponent(url.lastPathComponent))
tableView.reloadData()
} catch {
print(error)
}
}
这篇关于PDFView不显示我的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!