从Facebook sdk获取个人资料图片网址

时间:2016-02-19 10:30:25

标签: json swift facebook-graph-api xcode7

我无法找到获取Facebook个人资料图片网址的解决方案。

当我通过Facebook登录时,我使用此方法通过Facebook图形api获取个人资料信息:

func getFBUserData() {
    // Create request for user's Facebook data
    let request = FBSDKGraphRequest(graphPath:"me", parameters:["fields": "id, name, first_name, middle_name, last_name, email, picture.type(large)"])

    // Send request to Facebook
    request.startWithCompletionHandler {

        (connection, result, error) in

        if error != nil {
            // Some error checking here
        }
        else {

            print(result)


            if let userData = result as? [String:AnyObject] {

                // Access user data
                let username = userData["first_name"] as! String


            }

        }

    }
}

我可以轻松地解析first_name,发送电子邮件等,但是当涉及到图片网址时,我不知道如何记录这些数据。 以下是我对Facebooks api的回应:

{
    email = "dailocle@hotmail.com";
    "first_name" = Loc;
    id = 123456765432457654;
    "last_name" = Le;
    "middle_name" = Dai;
    name = "Loc Dai Le";
    picture =     {
        data =         {
            "is_silhouette" = 0;
            url = "https://scontent.xx.fbcdn.net/hprofile-xat1/v/t1.0-1/p200x200/12246670_10207807554127440_7406917875415466413_n.jpg?oh=d394b200091f8973645300054e6b32b8&oe=572E6626";
        };
    };
}

3 个答案:

答案 0 :(得分:1)

使用FBSDKProfile类的 imageURLForPictureMode:size 便捷方法。

let pictureURL = FBSDKProfile().imageURLForPictureMode(.Normal, size: CGSize(width: 100, height: 100))

答案 1 :(得分:0)

<强>更新

self.dict = result as NSDictionary               
        println(self.dict)
        NSLog(self.dict.objectForKey("picture")?.objectForKey("data")?.objectForKey("url") as String)

请试试这个

答案 2 :(得分:0)

这就是我的所作所为:

func getProfilePicture(account: ACAccount, handleSuccess: UIImage -> (), finally: () -> ()) {
    let r = SLRequest(forServiceType: SLServiceTypeFacebook, requestMethod: .GET, URL: NSURL(string: "https://graph.facebook.com/v2.5/me/picture")!, parameters: nil)
    r.account = account
    r.performRequestWithHandler({(data, urlresponse, error) in
        if error !== nil {
            //return
        } else if urlresponse.statusCode == 200 {
            handleSuccess(UIImage(data: data)!)
        } else {
            print("Unexpected status code \(urlresponse.statusCode)")
        }
        finally()
    })
}

如果你真的想要,你可以像原来的那样在原始请求上获取图片,但这是很多数据,所以这不是我接近它的方式。我想在你的情况下,这意味着UIImage(data: userData["picture"]),尽管你可能需要将它转换为NSData。