这是最新版本的最后一个正确语法(AlamofireImage
2.4.0 与Alamofire
3.3 的依赖项):
import AlamofireImage
var ext: String! = "jpeg"
self.ext = "png"
Alamofire.request(.GET, "https://httpbin.org/image/png")
.responseImage { response in
debugPrint(response)
print(response.request)
print(response.response)
debugPrint(response.result)
if let image = response.result.value {
print("image downloaded: \(image)") // example: background.png
var imgData: NSData!
switch self.ext {
case "png":
imgData = NSData(data: UIImagePNGRepresentation(image)!)
case "jpeg":
imgData = NSData(data: UIImageJPEGRepresentation((image), 1)!)
default:
break
}
if let data = imgData {
print("Size of Image: \(data.length) bytes")
}
}
}
您还可以使用一般的Alamofire框架,正是这个示例(下载文件或恢复已经在进行中的下载)。
Alamofire.download(.GET, "https://httpbin.org/stream/100") { temporaryURL, response in
let fileManager = NSFileManager.defaultManager()
let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let pathComponent = response.suggestedFilename
return directoryURL.URLByAppendingPathComponent(pathComponent!)
}
Alamofire.download(.GET, "https://httpbin.org/stream/100", destination: destination)
.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
print(totalBytesRead)
// This closure is NOT called on the main queue for performance
// reasons. To update your ui, dispatch to the main queue.
dispatch_async(dispatch_get_main_queue()) {
print("Total bytes read on main queue: \(totalBytesRead)")
}
}
.response { _, _, _, error in
if let error = error {
print("Failed with error: \(error)")
} else {
print("Downloaded file successfully")
}
}