2015-02-08 129 views
2
应用支持

的Alamofire自述表明您可以下载并保存这样的文件:下载图像使用Alamofire并保存到Mac上

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

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

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

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

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

我如何与Alamofire的suggestedDownloadDestination使用这个不清楚。

任何想法?

回答

3

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

啊,辉煌!非常感谢您的帮助。虽然我不得不删除'destination:',因为Xcode抛出了一个无关参数的警告: 'Alamofire.download(.GET,imgPath,{_ in newPath})' – 2015-02-08 07:23:42

+0

啊是的。回答编辑。很高兴它的工作。 – 2015-02-08 07:26:49