ios - Swift PDFDocument 为文件和 url 返回 nil

标签 ios swift pdf fatal-error ios-pdfkit

我想在 iOS 应用程序中使用 PDFView 显示 PDF 文件。我在我的项目目录中添加了一个名为 merlin.pdf 的文件。我还检查了 merlin.pdf 是否包含在构建阶段的 Copy Bundle Resources 中。但是,PDFDocument 仍然返回零。这是我正在使用的代码:

import UIKit
import PDFKit

class ReaderViewController: UIViewController {

    // MARK: - Properties
    @IBOutlet weak var pdfView: PDFView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let url = Bundle.main.url(forResource: "merlin", withExtension: "pdf")
        let pdfDocument = PDFDocument(url: url!)

        pdfView.document = pdfDocument
        pdfView.displayMode = PDFDisplayMode.singlePageContinuous
        pdfView.autoScales = true


        // Do any additional setup after loading the view.
    }
}

fatal error 在 pdfView.document = pdfDocument 处抛出。

然后,我尝试了 PDF 文件的在线链接。调试时,我可以看到 let pdfDocument = PDFDocument(url: url!) 正在从互联网下载文件。但是,它又像上次一样失败了。这是代码:

import UIKit
import PDFKit

class ReaderViewController: UIViewController {

    // MARK: - Properties
    @IBOutlet weak var pdfView: PDFView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let url = URL(string: "https://arxiv.org/pdf/1803.10760.pdf")
        let pdfDocument = PDFDocument(url: url!)

        pdfView.document = pdfDocument
        pdfView.displayMode = PDFDisplayMode.singlePageContinuous
        pdfView.autoScales = true


        // Do any additional setup after loading the view.
    }
}

我该怎么办?

最佳答案

也许您错过了在界面生成器中为您的 pdfView 导出设置 View 的类? enter image description here

因为您的代码很好并且适合我。

关于ios - Swift PDFDocument 为文件和 url 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54131762/

相关文章:

html - 使用 NReco (WkHtmlToPdf) 以单位设置页面大小

iOs:如何考虑应用程序激活时收到的所有通知

ios - 如何在iOS swift中基于一个字段过滤所有字段

ios - 在iOS应用程序中显示3D图表

ios - 快速检查表格 View 的滚动方式

android - 在 Android 中启动特定应用

iphone - pdf文件文本阅读和搜索

ios - 在 ios 中使用 Alamofire 在 collectionView 中显示图像

ios - 将缩略图附加到通知时,文件去了哪里?

ios - Firebase 中的观察者未在 swift 应用程序中被调用