swift - PDFView 不显示我的 PDF

标签 swift pdf

我无法让我的 pdfView 显示来自 URL 的 PDF。能拿到文件,根据控制台肯定是存在的。这是我所做的:

首先,我声明一个文档选择器:

 @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 和文档的名称:

func documentPicker(_ controller: UIDocumentPickerViewController,
                    didPickDocumentAt url: URL) {
    print ("Picker's url is \(url)")
    theName = String(describing:url.lastPathComponent)
    books.append(theName)
    tableView.reloadData()
}

然后,我将名称附加到文档目录路径:

fileIsFromService = true
indexPoint = indexPath.row
let DocumentDirectory = try! FileManager.default.url(for:  .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
docURL = DocumentDirectory.appendingPathComponent(theName)

然后我确保该文件存在,并且确实存在:

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:

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 的 subview 。没有变量是零。我怀疑我需要从 docURL 中获取数据,但我应该把它写在哪里?

最佳答案

问题是您导入的 pdf 只是一个临时文件。您需要将临时文件 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)
    }
}

关于swift - PDFView 不显示我的 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48133840/

相关文章:

ios - 尝试访问自定义 UiView 中的高度属性,遇到 "height is a get-only property"错误

Swift 4,仅使用 "input"实例变量子类化 CIFilter 崩溃

ios - Swift - 方法执行乱序

pdf - Ghostscript:将 PDF 转换为 CMYK,保留文本的纯黑色

android - 在 Android 应用程序中查看本地 PDF 文件

ios - 如何按百分比值填充圆圈颜色?

java - PDFBox:将pdf页面转换为图像的问题

phpDoc PDF模板

c# - 下载 html 格式的 pdf 时,使用第三方工具将 html 页面转换为 pdf。出现错误-转换错误 : Could not open url

ios - UITableView 重新加载问题