从Alamofire下载请求访问文件



因此,我使用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)")
            })

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

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)
  }

最新更新