gpt4 book ai didi

ios - PDFDocument 未从 URL 加载文件

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

我正在使用 PDFKit 通过 pdfView.document = document 函数在 PDFView 中显示 PDF 文件。

问题在于,我也在使用 UIDocumentPicker API,每当我从 didPickDocumentAt url 委托(delegate)方法检索文档 url 时,url 字符串均采用以下形式:

文件:///private/var/mobile/Containers/Data/Application/09281711-BFB5-4B74-94A1-473F9B460EBD/tmp/com.ivancantarino.Cozynote-Inbox/Documento%20PDF.pdf

如果我稍后尝试使用 PDFKit 预览该文档,它不会实例化该文档本身。

这是一个示例代码:

let pdfView = PDFView()
pdfView.autoScales = true
view.addSubview(pdfView)
pdfView.frame = view.frame

// ... getting the document url, which comes in the following form:
// file:///private/var/mobile/Containers/Data/Application/09281711-BFB5-4B74-94A1- 473F9B460EBD/tmp/com.ivancantarino.Cozynote-Inbox/Documento%20PDF.pdf
// I'm saving the URL as a string, due to the databse entry
let url: String = ...
let document = PDFDocument(url: url)
pdfView.document = document

document变量始终为 nil,它无法将 url 识别为合法路径。

我在这里做错了什么,还是有办法以任何不同的方式做到这一点?

谢谢。

最佳答案

委托(delegate):- UIDocumentInteractionControllerDelegate

   func downloadPdf(){
// Create destination URL

let documentsUrl:URL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last as URL?)!
let destinationFileUrl = documentsUrl.appendingPathComponent(fileName+".pdf")

//Create URL to the source file you want to download

let strUrl = "Your api URL"
let pdf = strUrl
let fileURL = URL(string: strUrl)
print("file url -> \(String(describing: fileURL))")

let isFileFound:Bool? = FileManager.default.fileExists(atPath: destinationFileUrl.path)
if isFileFound == true
{
if pdf != "" {
UIApplication.shared.openURL(URL(string: pdf)!)
}
}
else{
SVProgressHUD.show(withStatus: "Downloading...")
let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig)
let request = URLRequest(url:fileURL!)

let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
if let tempLocalUrl = tempLocalUrl, error == nil {
// Success
if let statusCode = (response as? HTTPURLResponse)?.statusCode {
SVProgressHUD.dismiss()
print("Successfully downloaded. Status code: \(statusCode)")

}
do{
if(FileManager.default.fileExists(atPath: destinationFileUrl.path))
{
try FileManager.default.removeItem(at: destinationFileUrl)
try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)

self.showFileWithPath(path: destinationFileUrl.path)
}
else
{
try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
self.showFileWithPath(path: destinationFileUrl.path)
SVProgressHUD.dismiss()
}
}
catch (let writeError)
{
print("Error creating a file \(destinationFileUrl) : \(writeError)")
}

} else {
SVProgressHUD.dismiss()
print("Error took place while downloading a file. Error description: %@", error?.localizedDescription ?? "Errorr.....");
}
}
task.resume()
}
}

func showFileWithPath(path: String){
DispatchQueue.main.async {
SVProgressHUD.dismiss()
let isFileFound:Bool? = FileManager.default.fileExists(atPath: path)
if isFileFound == true
{
let viewer = UIDocumentInteractionController(url: URL(fileURLWithPath: path))
viewer.delegate = self
//viewer.presentOpenInMenu(from: self.view.frame, in: self.view, animated: true)
viewer.presentPreview(animated: true)
}
}
}

func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self
}

关于ios - PDFDocument 未从 URL 加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59570729/

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