gpt4 book ai didi

ios - 使用 pdfKit 的目录中 pdf 的路径

转载 作者:行者123 更新时间:2023-11-28 14:42:55 33 4
gpt4 key购买 nike

我开始使用 PDFKit,在根目录中有一个 pdf 文件,它使用以下代码:

if let path = Bundle.main.path(forResource: "mypdf1", ofType: "pdf") {
let url = URL(fileURLWithPath: path)
if let pdfDocument = PDFDocument(url: url) {
pdfView.autoScales = true
pdfView.displayMode = .singlePageContinuous
pdfView.displayDirection = .vertical
pdfView.document = pdfDocument
print(path)
}
}

但是如果我更改目录中的 pdf 文件,例如“mydirectory”,它不起作用,我的代码如下:

if let path = Bundle.main.path(forResource: "mypdf1", ofType: "pdf", inDirectory: "mydirectory") {
let url = URL(fileURLWithPath: path)
if let pdfDocument = PDFDocument(url: url) {
pdfView.autoScales = true
pdfView.displayMode = .singlePageContinuous
pdfView.displayDirection = .vertical
pdfView.document = pdfDocument

}
}

解决路径问题的任何建议。


更新

按照建议,尝试了下面的代码,但是我也看不到PDF。

if let documentURL = Bundle.main.url(forResource: "mypdf1", withExtension: "pdf", subdirectory: "mydirectory") {
if let document = PDFDocument(url: documentURL) {
pdfView.autoScales = true
pdfView.backgroundColor = UIColor.lightGray
pdfView.document = document
}
}

最佳答案

您从子目录读取 pdf 的代码是正确的,当我读到它们在路径中得到一个 nil 时,这是肯定的,因为您的容器文件夹没有正确创建。您可以看到黄色和蓝色文件夹,您的容器文件夹应该是蓝色的。

要制作蓝色文件夹,您应该执行以下步骤:

  • 准备包含文件的文件夹结构。
  • 将该文件夹拖到 xcode 中,即项目导航面板。
  • 选择“为任何添加的文件夹创建文件夹引用”选项。
  • 最后点击添加。

您将获得蓝色文件夹,您的代码可以读取 pdf。

There are two types of folders in Xcode: groups and folder references. You can use groups to organize files in your project without affecting their structure on the actual file system. This is great for code, because you’re only going to be working with your code in Xcode. On the other hand, groups aren’t very good for resource files.

On any reasonably complicated project, you’ll usually be dealing with dozens – if not hundreds – of asset files, and those assets will need to be modified and manipulated from outside of Xcode, either by you or a designer. Putting all of your resource files in one flat folder is a recipe for disaster. This is where folder references come in. They allow you to organize your files into folders on your file system and keep that same folder structure in Xcode.

蓝色用于表示“文件夹引用”。

关于ios - 使用 pdfKit 的目录中 pdf 的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50428411/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com