objective-cのfacebookSDKで友達の写真を取得する方法②
前回の記事で書いた続き。
前回はfqlを使ってfacebookから友達の最新の写真を取得する方法を行なったため、今回はその取得した写真をtableViewCellに同期で表示する方法について。非同期についてはまた次回の方がいいかなぁ。
まず、取得されるデータは
- (void)request:(FBRequest *)request didLoad:(id)result;
に入る。写真の順番は最新の降順で来るため、tableViewCellを上から順番に入れて行けば良い。また、セクション数:50、セル数:2で行なった。
先ほどのメソッドに
self.picFriendsPhotos = [[NSMutableDictionaryalloc]init];
int count = 0;
id result = result_data_;
for (int i = 0; i < [result count] - 5; i++) {
//resultのsrc(URL)からimageを取得する
pic = [UIImageimageWithData:[NSDatadataWithContentsOfURL:
[NSURLURLWithString:[[result objectAtIndex:count] objectForKey:@"src"]]]];
NSUInteger newIndex[] = {count, 0}; // count番目のセクションの1行目
//各indexpathを作成
NSIndexPath *newPath = [[NSIndexPath alloc] initWithIndexes:newIndex length:2];
NSLog(@"indexPath : %@ , count : %d",newPath,count);
//pathをキーに取得したimageを保存
[self.picFriendsPhotos setObject:pic forKey:newPath];
count++;
}
と書く。
resultのデータが50個くるため、それをfor分で回して、各セクションの1行目のセルからNSIndexPathを作成し、それをキーにして取得したimageをセットする。そしたら、
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath内のメソッドで
if([picFriendsPhotos_ objectForKey:indexPath]) {
cell.imageView.image = [picFriendsPhotos_ objectForKey:indexPath];
}
とやると表示される。これだと画像データを取得中は固まってしまう。画像の大きさは130x130?あたりなのでcellの大きさは変えておくと良い。
次回はGCDを用いた非同期について書きます!