【问题标题】:How to display pdf from network in iOS [duplicate]如何在iOS中显示来自网络的pdf [重复]
【发布时间】:2017-11-06 16:59:55
【问题描述】:

目前我正在使用 QuickLook 模块从网络打开 pdf,但它在控制台中显示一个空白页面,并显示错误“无法为 url 发出文件扩展名: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)
 }
}

【问题讨论】:

    标签: ios swift xcode quicklook


    【解决方案1】:

    您需要先将文件保存到磁盘,然后才能呈现 pdf。如果文件位于远程位置,则无法使用 QuickLook 显示它。该文件保存在临时目录中。这是一个示例视图控制器,展示了它是如何完成的。

    斯威夫特 5:

    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(fileUrl 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
        }
    }
    

    斯威夫特 3:

    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(string: "")
    
        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 = Data(contentsOf: itemURL)
    
                // Give the file a name and append it to the file path
                fileURL = FileManager().temporaryDirectory.appendingPathComponent("sample.pdf")
                // Write the pdf to disk
                try data?.write(to: fileURL!, options: .atomic)
    
                // Make sure the file can be opened and then present the pdf
                if QLPreviewController.canPreview(fileUrl 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
        }
    }
    

    这是模拟器中显示的文件。使用仅包含该代码的示例项目。

    【讨论】:

    • 首先你应该使用 Data 而不是 NSData 从 Swift3 开始。其次不要使用 Data(contentsOf: URL) 同步加载非资源文件 url。如果没有互联网连接,它会冻结应用程序。第三,您可以只使用临时目录,而不是在显示后手动删除文件。检查今天早些时候提出并回答的重复问题。 stackoverflow.com/questions/47140314/…
    • 顺便说一句,当覆盖 viewDidAppear 方法时,您应该调用 super.viewDidAppear(animated) 而不是 super.viewDidLoad()
    猜你喜欢
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 2012-04-28
    • 1970-01-01
    相关资源
    最近更新 更多