iOS Swift Facebook SDK 4获取用户个人资料图片输出“问号”

时间:2015-08-07 04:41:53

标签: ios facebook swift

我不是编程专家,但我修改了代码以从Facebook SDK获取用户个人资料图片。问题是我在图像中得到一个问号(profilePicture.image)。谁能告诉我这里发生了什么?

另外,我不知道如何在viewController中调用此函数来获取图像?目前我在函数内部直接添加了profilePicture.image。

   func getProfPic(fid: String) -> UIImage? {
            if (fid != "") {
                var imgURLString = "http://graph.facebook.com/" + fid + "/picture?type=large" //type=normal
                var imgURL = NSURL(string: imgURLString)
                var imageData = NSData(contentsOfURL: imgURL!)
                var image = UIImage(data: imageData!)
                profilePicture.image = image // Returned image is Question mark 
                return image
            }
            return nil
        }

1 个答案:

答案 0 :(得分:5)

创建字典:

class ViewController: UIViewController {
    var dict : NSDictionary!
}

获取数据:

if((FBSDKAccessToken.currentAccessToken()) != nil){
    FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
        if (error == nil){
            self.dict = result as NSDictionary               
            println(self.dict)
            NSLog(self.dict.objectForKey("picture")?.objectForKey("data")?.objectForKey("url") as String)
        }
    })
}

输出应为:

{
    email = "ashishkakkad8@gmail.com";
    "first_name" = Ashish;
    id = 910855688971343;
    "last_name" = Kakkad;
    name = "Ashish Kakkad";
    picture =     {
        data =         {
            "is_silhouette" = 0;
            url = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xfa1/v/t1.0-1/s200x200/22501_915701971820048_9046303472199214595_n.jpg?oh=f3b3564f1450c13332b3067a135cad5d&oe=55C71792&__gda__=1443571904_c4667dcb08d85682edfd77a90ee9c3ab";
        };
    };
}
2015-05-25 22:12:34.015 SwiftFB[2713:7830] https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xfa1/v/t1.0-1/s200x200/22501_915701971820048_9046303472199214595_n.jpg?oh=f3b3564f1450c13332b3067a135cad5d&oe=55C71792&__gda__=1443571904_c4667dcb08d85682edfd77a90ee9c3ab

从网址转换图片

if let url = NSURL(string: "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xfa1/v/t1.0-1/s200x200/22501_915701971820048_9046303472199214595_n.jpg?oh=f3b3564f1450c13332b3067a135cad5d&oe=55C71792&__gda__=1443571904_c4667dcb08d85682edfd77a90ee9c3ab") {
    if let data = NSData(contentsOfURL: url){
        yourImageview.image = UIImage(data: data)
    }
}