gpt4 book ai didi

cocoa - 在 Mac 上使用 Alamofire 下载图像并保存到 Application Support

转载 作者:可可西里 更新时间:2023-11-01 01:06:22 26 4
gpt4 key购买 nike

Alamofire 自述文件表明您可以下载并保存这样的文件:

let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)

Alamofire.download(.GET, "http://httpbin.org/stream/100", destination: destination)

...但是我想更改它的保存位置。如何将 .DocumentDirectory 更改为我的应用程序在 Mac OS X 上的 Application Support 路径?

我可以像这样生成我的应用程序的本地 NSURL 路径:

let path = NSFileManager.defaultManager().URLsForDirectory(.ApplicationSupportDirectory, inDomains: .UserDomainMask)[0] as NSURL
let newPath = path.URLByAppendingPathComponent("MyApp/user-profile.png")

我不清楚如何将它与 Alamofire 的 suggestedDownloadDestination 一起使用。

有什么想法吗?

最佳答案

suggestedDownloadDestination 将尝试查找可用目录,但在您的情况下,您已经知道完整路径,因此不需要它。

只需用你的路径创建一个闭包:

let path = NSFileManager.defaultManager().URLsForDirectory(.ApplicationSupportDirectory, inDomains: .UserDomainMask)[0] as NSURL
let newPath = path.URLByAppendingPathComponent("MyApp/user-profile.png")
Alamofire.download(.GET, "http://httpbin.org/stream/100", { _ in newPath })

关于cocoa - 在 Mac 上使用 Alamofire 下载图像并保存到 Application Support,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28390937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com