【发布时间】:2015-09-29 05:03:11
【问题描述】:
我正在从 instagram 抓取图像并收到诸如“http://instagram.com/p/1JiWygQUSu/media/?size=l”之类的网址,问题是此网址指向“https://scontent-lax3-1.cdninstagram.com/hphotos-xfa1/t51.2885-15/e15/1527608_451589571655110_1239072675_n.jpg”我需要文件扩展名才能在用户设备上正确缓存图像。
第一个 url 重定向到第二个。我需要一种从第一个网址检索第二个网址的方法。
尝试使用此代码,但它只返回原始网址:
let urlString = "http://instagram.com/p/1JiWygQUSu/media/?size=l"
let request: NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string:urlString)!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringCacheData, timeoutInterval: 15.0)
request.HTTPMethod = "HEAD"
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {
(response, data, error) in
print("instaResponse \(response?.URL)")
})
【问题讨论】:
-
只需从重定向的url中提取lastPathComponent
-
你是如何抓取这些图像的?你在使用像 AFNetworking 这样的库吗?你在使用 NSURLSession 吗?
-
我正在使用 nsurlsession