2013-12-21 15 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中? –

相關問題