【发布时间】:2016-02-26 06:29:27
【问题描述】:
我正在尝试下载图片。
这是图片的链接
https://www.google.it/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png
我所做的是:
let url = NSURL(string: "https://www.google.it/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png")
let session = NSURLSession.sharedSession()
let task = session.downloadTaskWithURL(url!, completionHandler: {(url, response, error) in
if let error = error {
print("error = \(error)")
}
if let response = response {
print("response = \(response)")
}
})
task.resume()
我真的有回应,我可以在日志中看到如下:
response = <NSHTTPURLResponse: 0x7fc1a1427060> { URL: https://www.google.it/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png } { status code: 200, headers {
"Cache-Control" = "private, max-age=31536000";
"Content-Length" = 13504;
"Content-Type" = "image/png";
Date = "Mon, 23 Nov 2015 23:57:55 GMT";
Expires = "Mon, 23 Nov 2015 23:57:55 GMT";
"Last-Modified" = "Fri, 04 Sep 2015 22:33:08 GMT";
Server = sffe;
"alt-svc" = "quic=\"www.google.com:443\"; p=\"1\"; ma=600,quic=\":443\"; p=\"1\"; ma=600";
"alternate-protocol" = "443:quic,p=1";
"x-content-type-options" = nosniff;
"x-xss-protection" = "1; mode=block";
} }
我的问题是响应中没有 数据 来获取实际图像。我曾经打电话给dataTaskWithRequest,它的clouser有这样的数据:
session.dataTaskWithRequest(request, completionHandler: {(data, response, error)
但是这里我没有找到数据,请问我哪里错了?
【问题讨论】:
标签: ios swift nsurlsession nsurlsessiondownloadtask