简体   繁体   English

调用contentsOfUrl构造函数时,NSData返回nil

[英]NSData returns nil when contentsOfUrl constructor is called

let myProfilePictureURL = NSURL(string: "http://graph.facebook.com/bobdylan/picture")
let imageData           = NSData(contentsOfURL: myProfilePictureURL!)

In the above code, imageData is nil after execution. 在上面的代码中,执行后imageDatanil Any idea, what could be wrong here? 任何想法,这可能是什么问题?

The issue is actually you don't have any data in that specified url. 问题实际上是您在指定的url中没有任何数据。 If you paste that on the browser, it redirects to some other url and loads the image/data. 如果将其粘贴到浏览器中,它将重定向到其他URL并加载图像/数据。 That's why you are getting nil there. 这就是为什么你在那里nil的原因。

Facebook always provides a absolute URL to your profile image. Facebook始终为您的个人资料图像提供绝对URL。 You can paste that url in any browser and will display that image. 您可以在任何浏览器中粘贴该URL,并将显示该图像。 Please check the image url that you are getting from Facebook. 请检查您从Facebook获得的图片网址。

You're not getting data because you're not trying to laod the image but the "page". 您没有得到数据,因为您不是在尝试对图像进行修饰,而是对页面进行修饰。 Using your link, I can see the image, and considering what's in the url i suggest you replace 使用您的链接,我可以看到图像,并考虑我建议您替换的网址中的内容

let myProfilePictureURL = NSURL(string:"http://graph.facebook.com/bobdylan/picture")

with this 有了这个

let myProfilePictureURL = NSURL(string:"https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpa1/v/t1.0-1/p50x50/63781_10154869387235696_5558117780840742364_n.jpg?oh=92218e0caca2cbaa28266ef30757d4e2&oe=55780E74&__gda__=1434574503_cdb40e50a7c5770845fbbb6098bf073a")

I got this URL by either copying what's in the browser navigation bar (on the top of the screen) or simply by right cliking and then clicking " Copy URL of Image ". 我可以通过复制浏览器导航栏中的内容(在屏幕顶部)或简单地通过右击然后单击“ Copy URL of Image ”来获得此Copy URL of Image

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM