2013-12-21 16 views
1

我一直坚持这几天,并在搜索Stackoverflow上的答案的高和低,并通过GitHub上的代码寻找,我在我的智慧结束。我如何使用Facebook配置文件照片填充我的UITableView

我想要一个Facebook的个人资料图片显示与UITableView中的其他数据。我使用下面的代码。分数显示,Facebook配置文件名称显示,但图像不显示。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"]; 


    [FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"%@/scores?fields=score,user", appID] parameters:nil HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { 

     if (result && !error) 
     { 
      for (NSDictionary *dict in [result objectForKey:@"data"]) 
      { 
       NSString *name = [[dict objectForKey:@"user"] objectForKey:@"name"]; 
       NSString *strScore = [dict objectForKey:@"score"]; 
       NSString *profilePictureID = [[dict objectForKey:@"user"] objectForKey:@"id"]; 
       NSString *profilePicture = [NSString stringWithFormat:@"%@/picture?width=80&height=80&redirect=false",profilePictureID]; 
       NSString *url = [[NSString alloc] initWithFormat:@"https://graph.facebook.com/%@/picture",profilePictureID]; 
       UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]]; 
       NSLog(@"The URL is %@. The image URL is %@", url, image); 
       [url release]; 
       NSLog(@"This message is in the UITableViewCell and will hopefully show the name %@ and the score %@ along with %@ as the profile picture URL.", name, strScore, profilePicture); 

       // DID NOT WORK - cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", image]]; 
       // DID NOT WORK - cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", profilePicturel]]; 

       cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", url]]; 

       cell.textLabel.text = [NSString stringWithFormat:@"%@", name]; 
       cell.detailTextLabel.text = [NSString stringWithFormat:@"Score: %@", strScore]; 

      } 
     } 

    }]; 

    return cell; 
} 

我的NSLog消息正在返回以下数据。

- The URL is https://graph.facebook.com/xxxxx/picture. The image URL is <UIImage: 0xdee1234> 

- This message is in the UITableViewCell and will hopefully show the name xxxxx and the score xxxxx along with xxxxx/picture?width=80&height=80&redirect=false as the profile picture URL. 

我已经尝试了十几种不同的方式来获得Facebook的个人资料照片工作,我觉得这人会认为它是第一次,我回来日志信息是这样的。

我将不胜感激任何帮助或建议如何让这项工作。谢谢!

回答

1

您已经获取的图像(如果存在的话),这条线:

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]]; 

因此,所有你需要做的是设置:

cell.imageView.image = image; 

方法imageNamed:只需要当您正在尝试从项目中的文件加载图像。

但有一点需要注意,NSDatadataWithContentsofURL:方法是同步的。你将用这种方法阻止主线程。首先完成所有这些工作,将数据存储在数组中,然后使用该数组作为数据源重新加载tableView。

+0

它的工作!摆脱imagedNamed的伎俩。你太棒了!我对这一切都很陌生。我来到这么近,我知道有人知道他们在做什么可以告诉我在几分钟内我做错了什么。谢谢!我很乐意去做你对阵列的建议。什么是最好的方式来做到这一点?我会用该数组创建另一个.h&.m文件还是将它放在当前文件的某个位置?这个数组是什么样的,我将如何去将数据拉回到tableView中? –

相关问题