如何使用 cocoa swift 显示 pdf(版本 3.1)
How to display a pdf with cocoa swift (version 3.1)
我正在尝试为 mac(不是 IOS)制作一个 PDF 查看器来完成作业,但我什至不知道如何让 PDF 真正显示出来。我们必须使用 PDFView 和 Quartz。
我在这个主题上看到的大多数教程都使用类似的东西:
view.setDocument(pdf)
但是swift说PDFView没有成员setDocument。我查看了文档 here,唯一看起来可行的是 setCurrentSelection,所以我尝试了:
import Cocoa
import Quartz
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
@IBOutlet weak var PDFV: PDFView!
func applicationDidFinishLaunching(_ aNotification: Notification) {
let fileURL:URL = (Bundle.main.url(forResource: "example", withExtension: "pdf")! as NSURL) as URL
let pdfDocument:PDFDocument = PDFDocument.self.init(url: fileURL as URL)!
let thing:PDFSelection = PDFSelection.self.init(document: pdfDocument)
PDFV.setCurrentSelection(thing, animate: true)
// Insert code here to initialize your application
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
但这会导致 window 在我 运行 它和 xcode 说时崩溃:
线程 1:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码 0x0)。有谁知道我的实际用途是什么?
确实没有setDocument
方法,但是有一个document
属性可以使用:
guard let pdfURL = Bundle.main.url(forResource: "test", withExtension: "pdf")
else { fatalError("PDF not found") }
guard let pdfDocument = PDFDocument(url: pdfURL)
else { fatalError("PDF document not created") }
pdfView.document = pdfDocument
此外,无需将 URL 转换为 NSURL,然后再转换回 URL。
在Swift4中,可以使用:
导入PDFKit
将您的 pdf 复制到您的项目
粘贴以下代码(测试为pdf文件名)
let pdfView = PDFView()
pdfView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(pdfView)
pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
pdfView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
guard let path = Bundle.main.url(forResource: "test", withExtension: "pdf") else { return }
if let document = PDFDocument(url: path) {
pdfView.document = document
}
我正在尝试为 mac(不是 IOS)制作一个 PDF 查看器来完成作业,但我什至不知道如何让 PDF 真正显示出来。我们必须使用 PDFView 和 Quartz。 我在这个主题上看到的大多数教程都使用类似的东西:
view.setDocument(pdf)
但是swift说PDFView没有成员setDocument。我查看了文档 here,唯一看起来可行的是 setCurrentSelection,所以我尝试了:
import Cocoa
import Quartz
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
@IBOutlet weak var PDFV: PDFView!
func applicationDidFinishLaunching(_ aNotification: Notification) {
let fileURL:URL = (Bundle.main.url(forResource: "example", withExtension: "pdf")! as NSURL) as URL
let pdfDocument:PDFDocument = PDFDocument.self.init(url: fileURL as URL)!
let thing:PDFSelection = PDFSelection.self.init(document: pdfDocument)
PDFV.setCurrentSelection(thing, animate: true)
// Insert code here to initialize your application
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
但这会导致 window 在我 运行 它和 xcode 说时崩溃: 线程 1:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码 0x0)。有谁知道我的实际用途是什么?
确实没有setDocument
方法,但是有一个document
属性可以使用:
guard let pdfURL = Bundle.main.url(forResource: "test", withExtension: "pdf")
else { fatalError("PDF not found") }
guard let pdfDocument = PDFDocument(url: pdfURL)
else { fatalError("PDF document not created") }
pdfView.document = pdfDocument
此外,无需将 URL 转换为 NSURL,然后再转换回 URL。
在Swift4中,可以使用:
导入PDFKit
将您的 pdf 复制到您的项目
粘贴以下代码(测试为pdf文件名)
let pdfView = PDFView() pdfView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(pdfView) pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true pdfView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true guard let path = Bundle.main.url(forResource: "test", withExtension: "pdf") else { return } if let document = PDFDocument(url: path) { pdfView.document = document }