gpt4 book ai didi

swift - PDFView 不显示我的 PDF

转载 作者:行者123 更新时间:2023-11-28 12:12:05 30 4
gpt4 key购买 nike

我无法让我的 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/

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