Rom*_*mar 0 xcode quicklook ios swift
目前我正在使用QuickLook模块从网络打开pdf,但它显示一个空白页面,错误"无法为网址发布文件扩展名:https://testing-xamidea.s3.amazonaws.com/flowchart/20171103182728150973368.pdf "在控制台.我猜QuickLook只能打开本地保存的Pdf文件.是否可以使用quicklook从网络加载pdf?.到目前为止这是我的代码 - {fileURL包含要加载pdf的url,也设置了委托等)
extension FlowchartVC:QLPreviewControllerDelegate,QLPreviewControllerDataSource {
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
let url : NSURL! = NSURL(string : fileURL)
return url
}
func previewControllerWillDismiss(_ controller: QLPreviewController) {
self.dismiss(animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
您需要先将文件保存到磁盘,然后才能显示pdf.如果文件位于远程位置,则无法使用QuickLook呈现它.然后,您可以在用户完成预览后删除该文件.这是一个示例视图控制器,显示它是如何完成的.
import UIKit
import QuickLook
class ViewController: UIViewController, QLPreviewControllerDataSource {
// Remote url pdf I found on google
let itemURL = URL(string: "https://www.ets.org/Media/Tests/GRE/pdf/gre_research_validity_data.pdf")!
var fileURL: URL?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let quickLookController = QLPreviewController()
quickLookController.dataSource = self
do {
// Download the pdf and get it as data
// This should probably be done in the background so we don't
// freeze the app. Done inline here for simplicity
let data = try Data(contentsOf: itemURL)
// Give the file a name and append it to the file path
fileURL = FileManager().temporaryDirectory.appendingPathComponent("sample.pdf")
if let fileUrl = fileURL {
// Write the pdf to disk in the temp directory
try data.write(to: fileUrl, options: .atomic)
}
// Make sure the file can be opened and then present the pdf
if QLPreviewController.canPreview(itemURL as QLPreviewItem) {
quickLookController.currentPreviewItemIndex = 0
present(quickLookController, animated: true, completion: nil)
}
} catch {
// cant find the url resource
}
}
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
return fileURL! as QLPreviewItem
}
}
Run Code Online (Sandbox Code Playgroud)
这是在模拟器中显示的文件.使用仅包含该代码的示例项目.
| 归档时间: |
|
| 查看次数: |
2252 次 |
| 最近记录: |