【问题标题】:Accessing Files from Alamofire Download Request从 Alamofire 下载请求访问文件
【发布时间】:2019-04-30 08:57:57
【问题描述】:

所以我使用 Alamofire 和这个请求发出了下载请求并返回图像、语音、视频,我能够通过 destinationURL 看到文件,但我的问题是如何将请求结果转换为我可以的数据使用,比如如果我得到了图像,如何将它添加到 ImageView 等等,我也担心每次打开页面时都会调用这个函数,即使文件是在文档中下载的,这不是要花一点时间吗?失去记忆?并影响性能??

        let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
        Alamofire.download(
            "url",
            method: .get,
            parameters: nil,
            encoding: JSONEncoding.default,
            headers:nil ,
            to: destination).downloadProgress(closure: { (progress) in
                //progress closure
            }).response(completionHandler: { (DefaultDownloadResponse) in
                //here you able to access the DefaultDownloadResponse
                //result closure
                print("*****DefaultDownloadResponse***** \(DefaultDownloadResponse.response) and \(DefaultDownloadResponse.resumeData) and \(DefaultDownloadResponse.destinationURL)")
            })

【问题讨论】:

  • 您应该为文件提供一些自定义的唯一名称,以便您可以使用 FileManagerdocument directory 获取。如果你使用FileManager,你可以检查文件是否已经存在。
  • 如果我从 DefaultDownloadResponse.destinationURL 获取路径,我会在模拟器中找到我的文件,我可以使用它吗?
  • 是的,但最好在 almofire 中也提供自定义名称,以便您轻松搜索特定文件。
  • 你能分享我的代码吗,因为我无法让它工作?
  • 从我的评论中得到一些想法后,添加您尝试过的代码。

标签: ios swift alamofire alamofire-request


【解决方案1】:

您必须这样做才能显示下载的文件!
你可以这样走:

extension ViewPreRegistrationViewController : UIDocumentInteractionControllerDelegate {


  func startDownload(Url:String) -> Void {
    headers = ["Authorization": "Token \(Auth.userToken!)"]
    let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
    Alamofire.download(
      Url,
      method: .get,
      encoding: JSONEncoding.default,
      headers: headers,
      to: destination).downloadProgress(closure: { (progress) in
        //progress closure
      }).response(completionHandler: { (DefaultDownloadResponse) in
        //here you able to access the DefaultDownloadResponse
        let docController = UIDocumentInteractionController(url: DefaultDownloadResponse.destinationURL!)
        docController.delegate = self
        docController.name = "show"
        docController.presentPreview(animated: true)
        //result closure
      })
  }



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


}

并使用:

  @IBAction func exportPdfButton(_ sender: Any) {
    let fullURL = "your url"
    startDownload(Url: fullURL)
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-14
    • 2021-07-07
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多