简体   繁体   English

获取iOS中其他Facebook用户的个人资料照片

[英]Get profile picture for other Facebook users in iOS

I'm trying to get the profile image for a Facebook user using the Graph API in the Facebook SDK for iOS but the example code from the website does not work for me. 我正在尝试使用iOS版Facebook SDK中的Graph API为Facebook用户获取个人资料图片,但是该网站上的示例代码对我不起作用。 https://developers.facebook.com/docs/graph-api/reference/user/picture/ https://developers.facebook.com/docs/graph-api/reference/user/picture/

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
    initWithGraphPath:@"/123456789/picture"
           parameters:nil
           HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
    // Insert your code here
}];

I get the following error in the console. 我在控制台中收到以下错误。

FBSDKLog: starting with Graph API v2.4, GET requests for //123456789/picture should contain an explicit "fields" parameter

From my understanding of the error message instead of parameters:nil I need to enter something like parameters:@{@"fields": @"id"} but whatever I try result in the completing handler is always nil. 根据我对错误消息的理解,而不是parameters:nil我需要输入一些类似parameters:@{@"fields": @"id"}但是我尝试完成处理程序的result始终为nil。 I don't know what to enter for the profile picture. 我不知道要为个人资料图片输入什么。

EDIT: I'm looking to get the profile picture for other users. 编辑:我正在寻找获取其他用户的个人资料图片。

Try This One:- 试试这个:

- (IBAction)btnFaceBook:(id)sender {



    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login setLoginBehavior:FBSDKLoginBehaviorBrowser];
    [login logInWithReadPermissions:@[@"public_profile",@"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
        if (error)
        {
            [[FBSDKLoginManager new]logOut];

        } else if (result.isCancelled)
        {
            // Handle cancellations
        }
        else
        {
            if ([result.grantedPermissions containsObject:@"email"])
            {
                [self getDataFromFB];
            }
        }
    }];
}


-(void)getDataFromFB
{
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view  animated:true];

    if ([FBSDKAccessToken currentAccessToken]) {
         [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:[NSDictionary dictionaryWithObject:@"political,picture.width(500).height(500),id,email,first_name,last_name,gender,name" forKey:@"fields"]]
     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
         {
             if (!error && [result count] != 0)
             {
                 [hud hideAnimated:true];
                 NSLog(@"%@", result);

             }
             else
             {
                  [hud hideAnimated:true];
                  [self displayAlertMesage:@"Some error has been occure." title:@"Oops!"];
             }
        }];
     }
 }

You can use the following code for getting the data from the facebook login:- 您可以使用以下代码从facebook登录名获取数据:-

-(void)getFacebookProfileInfos { FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]initWithGraphPath:@"me" parameters:@{@"fields": @"id,email,name,birthday,first_name,last_name,gender,picture.type(large)"}]; -(void)getFacebookProfileInfos {FBSDKGraphRequest * requestMe = [[FBSDKGraphRequest alloc] initWithGraphPath:@“ me”参数:@ {@“ fields”:@“ id,电子邮件,名称,生日,名字,姓氏,性别,图片。大)”}];

FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];

[connection addRequest:requestMe completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {

    if(result)
    {
        dictUserData = [[NSDictionary alloc]init];
        dictUserData=result;
    }
}];
[connection start];

} }

you will get all the data in the "dictUserData" dictionary and from that you will access the facebook profile in this way:- 您将在“ dictUserData”词典中获取所有数据,并通过这种方式访问​​Facebook个人资料:-

UIImage * imgProfile = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", dictUserData[@"picture"][@"data"][@"url"]]]]]; UIImage * imgProfile = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@“%@”,dictUserData [@“ picture”] [@“ data”] [@“ url”]]]]]]]; NSString * strImageData = [self imageToNSString:imgProfile]; NSString * strImageData = [自身imageToNSString:imgProfile];

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

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