【发布时间】:2014-11-28 02:18:23
【问题描述】:
我学得很快,我正在尝试开发一个下载图像的 OS X 应用程序。
我已经能够将我正在寻找的 JSON 解析为 URL 数组,如下所示:
func didReceiveAPIResults(results: NSArray) {
println(results)
for link in results {
let stringLink = link as String
//Check to make sure that the string is actually pointing to a file
if stringLink.lowercaseString.rangeOfString(".jpg") != nil {2
//Convert string to url
var imgURL: NSURL = NSURL(string: stringLink)!
//Download an NSData representation of the image from URL
var request: NSURLRequest = NSURLRequest(URL: imgURL)
var urlConnection: NSURLConnection = NSURLConnection(request: request, delegate: self)!
//Make request to download URL
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: { (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
if !(error? != nil) {
//set image to requested resource
var image = NSImage(data: data)
} else {
//If request fails...
println("error: \(error.localizedDescription)")
}
})
}
}
}
所以此时我将我的图像定义为“图像”,但我在这里未能掌握的是如何将这些文件保存到我的本地目录。
非常感谢您对此事的任何帮助!
谢谢,
tvick47
【问题讨论】:
标签: json macos cocoa swift osx-yosemite