1

我的应用程序有文件下载选项,它使用 alamofire 下载方法下载文件。下载完成后,我需要呈现文件的预览而不将其保存到内部/云存储中。我怎样才能实现这个类似whatsapp的功能,在下载文件后显示预览。

func downloadFile(fileUrl: URL) {
    let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)

    Alamofire.download(fileUrl, to: destination)
        .response(completionHandler: { (downloadResponse) in
            self.dic.url = downloadResponse.destinationURL
            self.dic.uti = downloadResponse.destinationURL!.uti
            let rect = CGRect(x: 0, y: 0, width: 100, height: 100)
            self.dic.presentOpenInMenu(from: rect, in: self.view, animated: true)
        })
}
4

2 回答 2

4

要呈现文件的预览,请使用 Apple 的QuickLook框架,该框架允许您嵌入大量文件类型的预览,包括 iWork 文档、Microsoft Office 文档、PDF、图像等,所有这些都无需编写太多代码。

首先,导入 QuickLook 框架,然后使您的视图控制器符合 QLPreviewControllerDataSource 协议。

参考:

  1. https://www.hackingwithswift.com/example-code/libraries/how-to-preview-files-using-quick-look-and-qlpreviewcontroller

  2. https://github.com/gargsStack/QLPreviewDemo

  3. https://www.appcoda.com/quick-look-framework/

代码:

class ViewController: UIViewController {
    var previewItem = URL!

    func downloadFile(fileUrl: URL) {
        let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)

        Alamofire.download(fileUrl, to: destination)
        .response(completionHandler: { (downloadResponse) in

            let previewController = QLPreviewController()
            previewController.dataSource = self
            self.previewItem = downloadResponse.destinationURL
            self.present(previewController, animated: true, completion: nil)
        })
    }
}

extension ViewController: QLPreviewControllerDataSource {
    func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
        return 1
    }

    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
       return self.previewItem as QLPreviewItem
    }
}
于 2019-01-10T12:17:05.490 回答
1

这是使用 Alamofire 的一种解决方案。有人可能会帮忙。

脚步:

  • Alamofire 拥有优秀的员工来直接下载并将您的文件保存/写入光盘。

  • 返回下载文件保存的路径。

  • 使用UIDocumentInteractionController传递文件路径

  • 然后呈现这个视图

     import Alamofire
    
         extension UIViewController : UIDocumentInteractionControllerDelegate{
    
    
         func downloadFileForPreview(fileName: String, fileExtension: String, filePath: String )  {
    
    
             let destination: DownloadRequest.DownloadFileDestination = { _, _ in
                 let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    
                 let fileWithExtension = "file.\(fileExtension)"
    
                 let fileURL = documentsURL.appendingPathComponent(fileWithExtension)
    
                 return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
             }
             //UtilitySwift.showUniversalLoadingView(true)
             Alamofire.download(filePath, to: destination).response {  response in
                 debugPrint(response)
             //UtilitySwift.showUniversalLoadingView(false)
    
                 if response.error == nil, let storeFilePath = response.destinationURL?.path {
                     //let image = UIImage(contentsOfFile: imagePath)
                     self.previewDocument(withFilePath: response.destinationURL)
                     print(storeFilePath)
    
                 }
    
                 else{
                     UtilitySwift.showErrorMessage(message: response.error?.localizedDescription ?? "Error occured when downloading" )
                     print(response.error?.localizedDescription ?? "")
                 }
    
             }
         }
    
    
         //  Converted to Swift 5.1 by Swiftify v5.1.29672 - https://objectivec2swift.com/
         func previewDocument(withFilePath filePath: URL?) {
    
             var documentInteractionController: UIDocumentInteractionController?
    
             if filePath != nil {
                 // Initialize Document Interaction Controller
                 if let filePath = filePath {
                     documentInteractionController = UIDocumentInteractionController(url: filePath)
                 }
    
                 // Configure Document Interaction Controller
                 documentInteractionController?.delegate = self as UIDocumentInteractionControllerDelegate
    
                 //if not possible to open
                 if !documentInteractionController!.presentPreview(animated: true) {
                     documentInteractionController?.presentOptionsMenu(from: CGRect.zero, in: self.view, animated: true)
                 }
    
             } else {
                 // error
                 print("file preview error")
             }
         }
         //UIDocumentInteractionControllerDelegate
         public func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
             self
         }
    
         }
    

从任何呼叫UIViewController

self.downloadFileForPreview(fileName: "file", fileExtension: fileExt ?? "", filePath: REPLACE_WITH_DOWNLOAD_URL)

在此处输入图像描述

于 2020-07-03T07:46:55.913 回答