【问题标题】:PDFViewer SWIFT 4 Xcode 10.3PDFViewer SWIFT 4 Xcode 10.3
【发布时间】:2019-12-09 05:28:46
【问题描述】:

这是我的代码。我有一个文件“dreamGirls.pdf”

import UIKit
import PDFKit

var pdfView : PDFView!

    func createPDFViewer() {
        let path = Bundle.main.path(forResource: "dreamGirls", ofType: "pdf")
        let url = URL(fileURLWithPath: path!)
        let pdfDocument = PDFDocument(url:url)

        self.pdfView.document = pdfDocument
        self.pdfView.autoScales = true
        self.pdfView.displayMode = .singlePageContinuous
        self.pdfView.displayDirection = .vertical

        let width = self.view.frame.width
        let height = self.view.frame.height - 100
        self.pdfView.frame = CGRect(x: 0, y: 100, width: width, height: height)
        self.pdfView.backgroundColor = .red
        self.view.addSubview(self.pdfView)
    }

我还尝试了以下方法:

func createPDFViewer() {

    let fileToShow = Bundle.main.url(forResource: "dreamGirls", withExtension: "pdf")
    let documentToShow = PDFDocument(url: fileToShow!)

    self.pdfView.document = documentToShow
    self.pdfView.autoScales = true
    self.pdfView.displayMode = .singlePageContinuous
    self.pdfView.displayDirection = .vertical

    let width = self.view.frame.width
    let height = self.view.frame.height - 100
    self.pdfView.frame = CGRect(x: 0, y: 100, width: width, height: height)
    self.pdfView.backgroundColor = .red
    self.view.addSubview(self.pdfView)
}

错误输出: 致命错误:在隐式展开可选值时意外发现 nil

pdf查看 PDF查看?无 无

我也试过了,同样的错误:


import UIKit
import PDFKit

class ViewController: UIViewController {

    var pdfView : PDFView?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        let fileToShow = Bundle.main.url(forResource: "dreamGirls", withExtension: "pdf")
        let documentToShow = PDFDocument(url: fileToShow!)

        self.pdfView!.document = documentToShow!
        self.pdfView!.autoScales = true
        self.pdfView!.displayMode = .singlePageContinuous
        self.pdfView!.displayDirection = .vertical

        let width = self.view.frame.width
        let height = self.view.frame.height - 100
        self.pdfView!.frame = CGRect(x: 0, y: 100, width: width, height: height)
        self.pdfView!.backgroundColor = .red
        self.view.addSubview(self.pdfView!)


    }


}

self pdfViewerTest.ViewController 0x00007fe538520580 UIKit.UIViewController UIViewController
pdfView PDFView?无 无 文件显示网址? "file:///Users/nacly/Library/Developer/CoreSimulator/Devices/C92B8B59-90C5-4509-A848-99D0B39A4469/data/Containers/Bundle/Application/7D0B63DF-F265-48DB-8887-D0563FD45FDA/pdfViewerTest.app/ dreamGirls.pdf" 一些 _url NSURL "file:///Users/nacly/Library/Developer/CoreSimulator/Devices/C92B8B59-90C5-4509-A848-99D0B39A4469/data/Containers/Bundle/Application/7D0B63DF-F265-48DB-8887-D0563FD45FDA/pdfViewerTest.应用程序/dreamGirls.pdf" 0x0000600002638120 documentToShow PDFDocument? 0x00006000000301d0 ObjectiveC.NSObject NSObject
宽度 CGFloat 高度 CGFloat

我还尝试了以下方法,以防 PDFView 未初始化:


import UIKit
import PDFKit

class ViewController: UIViewController {

    var pdfView : PDFView?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.pdfView = PDFView()
        let fileToShow = Bundle.main.url(forResource: "dreamGirls", withExtension: "pdf")
        let documentToShow = PDFDocument(url: fileToShow!)

        self.pdfView!.document = documentToShow!
        self.pdfView!.autoScales = true
        self.pdfView!.displayMode = .singlePageContinuous
        self.pdfView!.displayDirection = .vertical

        let width = self.view.frame.width
        let height = self.view.frame.height - 100
        self.pdfView!.frame = CGRect(x: 0, y: 100, width: width, height: height)
        self.pdfView!.backgroundColor = .red
        self.view.addSubview(self.pdfView!)


    }

它返回以下错误:

pdfViewerTest[16863:1994476] * 由于未捕获的异常“CALayerInvalidGeometry”而终止应用程序,原因:“CALayer 位置包含 NaN:[nan nan]” * 首先抛出调用栈:

【问题讨论】:

  • 您需要通过执行来查看 url 和 pdfDocument 的值。其中一个或两个都为空。在我看来,SWIFT 非常乐意将 path 和/或 url 解包为 null 而不会出现错误。但是当它强制将整个东西解开为 PDFView 时,它就会爆炸。我怀疑 Bundle.main.path 和 .url 没有返回你所期望的,可能需要一些编辑。
  • 我已经尝试了一些保护/让的东西,但它仍然不起作用。
  • 我相信这个例子可以被进一步淘汰。
  • 你建议我如何更好地格式化它?我有 4 次明确的尝试,有错误消息,甚至还有错误的屏幕截图……我还能做什么?

标签: swift4 ios-pdfkit xcode10.3


【解决方案1】:

好的,我解决了。由于几何形状不好,我不得不在“viewDiDAppear”而不是“viewDiDLoad”中进行调用。

【讨论】:

    猜你喜欢
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    相关资源
    最近更新 更多