2015-02-08 127 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