0

好的,所以当我尝试接收用户个人资料图片时,图片返回一个带有问号的白色框?

这是我的代码:

func getProfilePic(fid: String) -> SKTexture? {
  let imgURL = NSURL(string: "http://graph.facebook.com/" + fid + "/picture?type=large")
  let imageData = NSData(contentsOfURL: imgURL!)
  let imageUI = UIImage(data: imageData!)
  let image = SKTexture(image: imageUI!)
  return image
}

func getFBUserData() {
  if((FBSDKAccessToken.currentAccessToken()) != nil) {
    FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email, picture"]).startWithCompletionHandler({ (connection, result, error) -> Void in
      if (error == nil){
        print(result)
        if let userData = result as? NSDictionary {
          personalUserID = userData["id"] as! String
        }
      } else {
        print("error")
      }
    })
  }
  picture.texture = getProfilePic("\(personalUserID)")

如何让它显示正确的图片?

4

1 回答 1

2

我猜你的个人资料图片不公开。现在您没有在请求中提供访问令牌,因此该请求被视为未经授权 - 因此您只能看到公众看到的内容。

要解决这个问题:

func getProfilePic(fid: String) -> SKTexture? {
    let imgURL = NSURL(string: "http://graph.facebook.com/" + fid + "/picture?type=large&access_token=" + FBSDKAccessToken.currentAccessToken().tokenString)
    let imageData = NSData(contentsOfURL: imgURL!)
    let imageUI = UIImage(data: imageData!)
    let image = SKTexture(image: imageUI!)
    return image
}

此外,您希望使用https当前的 API 版本v2.5来发出请求,否则当 Facebook 进行更改时,您的代码可能会在任何一秒内中断。所以,考虑到这一点:

func getProfilePic(fid: String) -> SKTexture? {
    let imgURL = NSURL(string: "https://graph.facebook.com/v2.5/" + fid + "/picture?type=large&access_token=" + FBSDKAccessToken.currentAccessToken().tokenString)
    let imageData = NSData(contentsOfURL: imgURL!)
    let imageUI = UIImage(data: imageData!)
    let image = SKTexture(image: imageUI!)
    return image
}

这应该够了吧。

于 2016-03-11T17:04:48.453 回答