2015-03-03 75 views
-1

我已经为Swift语言创建了OSX按钮。我希望该按钮应该在我的Mac中下载图像。 我应该使用什么代码来下载该图像?如何使用swift语言在OS X中下载图像

下面是我使用的代码:

@IBAction func buttonPressed(sender: NSButton) { 
    buttonPresses+=1 
    theLabel.stringValue = "You've pressed the button \n \(buttonPresses) times!" 
    theButton.title = "Download\(buttonPresses)" 
} 

回答

0

Alamofire是下载图像的简单方法:

Alamofire.request(.GET, url).response() { request, response, data, error in 
    var image: UIImage? 
    if nil == error { 
     if let imageData = data as? NSData { 
      image = UIImage(data: imageData) 
     } 
    } 
    self.imageView.image = image 
} 
+0

我都用过,但我没有得到又 – 2015-03-03 09:47:15

+0

有一个错误 - -unresloved identifier – 2015-03-03 09:54:19

+0

首先安装迦太基或CocoaPods的Alamofire,然后在源文件的开头添加'import Alamofire'。示例代码中的'self.imageView'只是一个指定UIImage的示例。请用代码中的其他任何东西替换'self.imageView'。 – 2015-03-03 10:45:37

相关问题