【发布时间】: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)")
})
【问题讨论】:
-
您应该为文件提供一些自定义的唯一名称,以便您可以使用
FileManager从document directory获取。如果你使用FileManager,你可以检查文件是否已经存在。 -
如果我从 DefaultDownloadResponse.destinationURL 获取路径,我会在模拟器中找到我的文件,我可以使用它吗?
-
是的,但最好在 almofire 中也提供自定义名称,以便您轻松搜索特定文件。
-
你能分享我的代码吗,因为我无法让它工作?
-
从我的评论中得到一些想法后,添加您尝试过的代码。
标签: ios swift alamofire alamofire-request