[英]Get Facebook API Profile Picture ios Swift
I am using interested in obtaining the following info from a user's fb profile : 我对从用户的fb个人资料获取以下信息感兴趣:
To do so I use the following code: 为此,我使用以下代码:
let accessToken = FBSDKAccessToken.currentAccessToken()
let req = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"picture.redirect(false),email,name,gender,age_range,birthday"], tokenString: accessToken.tokenString, version: nil, HTTPMethod: "GET")
req.startWithCompletionHandler({ (connection, result, error : NSError!) -> Void in
if(error == nil)
{
print("result \(result)")
}
else
{
print("error \(error)")
}
}) }
This returns: 返回:
result {
"age_range" = {
min = 21;
};
email = "dedewdd@gmail.com";
gender = male;
id = xxxxxxxxxxxx;
name = "John Smith";
picture = {
data = {
"is_silhouette" = 0;
url = "https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/dwqdwddqwdwqdw";
};
};
}
The problem with this is that this image is 50X50 pixels, I would like to obtain a link to a larger image. 问题是此图像为50X50像素,我想获得指向较大图像的链接。
So I use Kingfisher to download the image but essentially the NSURL is the key to getting a larger image. 因此,我使用Kingfisher下载图像,但从本质上讲,NSURL是获取更大图像的关键。 IF this doesnt work, then try changing the "50x50" in the url to "250x250" and see if that works 如果此操作无效,请尝试将网址中的“ 50x50”更改为“ 250x250”,看看是否可行
if FBSDKAccessToken.currentAccessToken() != nil {
let userID = FBSDKAccessToken.currentAccessToken().userID
if(userID != nil) //should be != nil
{
print(userID)
}
let URL = NSURL(string: "http://graph.facebook.com/\(userID)/picture?type=large")!
profileImage.kf_setImageWithURL(URL)
}else{
}
}
Try this i hope it would be helpful!! 试试这个,希望对您有所帮助!!
@IBAction func FacebookLogin(sender: AnyObject) {
var message = String()
if Reachability.isConnectedToNetwork() == true {
let loginView : FBSDKLoginManager = FBSDKLoginManager()
loginView.loginBehavior = FBSDKLoginBehavior.Web
if (FBSDKAccessToken .currentAccessToken() != nil) {
loginView.logOut()
}else{
loginView.logInWithReadPermissions(["email","public_profile"], fromViewController: self, handler: { (result:FBSDKLoginManagerLoginResult!, error:NSError!) in
if (error != nil){
print("Login Process Eroor!"+error.localizedDescription)
}else if result.isCancelled{
print("User cancled Login")
}else{
print("Login Success")
if result.grantedPermissions .contains("email"){
self.fetchUserInfo()
}else{
message = message.stringByAppendingString("Facebook email permission error")
WebService.ShowToastMessage(message, viewcontroller: self)
}
}
})
}
} else {
message = message.stringByAppendingString("Make sure your device is connected to the internet.")
WebService.ShowToastMessage(message, viewcontroller: self)
}
}
func fetchUserInfo() -> Void {
if (FBSDKAccessToken .currentAccessToken() != nil) {
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id,gender,birthday,email,name,picture.width(480).height(480)"])
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
print("Error: \(error.localizedDescription)")
}
else
{
print("fetched user: \(result)")
let id : NSString = result.valueForKey("id") as! String
let name:NSString = result.valueForKey("name") as! String
let gender:NSString = result.valueForKey("gender") as! String
let email:NSString = result.valueForKey("email") as! String
print("User ID is: \(id)")
print("User email is:\(email)")
print("User name is :\(name)")
print("User gender is :\(gender)")
}
})
}
}
According to the facebook docs 根据facebook文档
https://developers.facebook.com/docs/graph-api/reference/user/picture/ https://developers.facebook.com/docs/graph-api/reference/user/picture/
You can pass width and height of image,following are the list of params.. 您可以传递图像的宽度和高度,以下是参数列表。
So you can try something like .. 因此,您可以尝试类似..
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{ @"fields" : @"id,name,picture.width(100).height(100)"}]startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
}] }]
Use this code for getting user id, name and profilepicture with one request : https://stackoverflow.com/a/45462114/1168602 使用此代码通过一个请求获取用户ID,名称和个人资料图片: https ://stackoverflow.com/a/45462114/1168602
Tested on xcode 8.3.3 在xcode 8.3.3上测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.