从Alamofire下载请求访问文件

问题描述 投票:0回答:1

所以我使用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)")
            })
ios swift alamofire alamofire-request
1个回答
0
投票

您可以通过这种方式:

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.presentPreview(animated: true)
        //result closure
      })
  }



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


}

和使用:

  @IBAction func exportPdfButton(_ sender: Any) {
    let fullURL = "your url"
    startDownload(Url: fullURL)
  }
© www.soinside.com 2019 - 2024. All rights reserved.