私は uitableview を持っており、各セルには Facebook の画像プロファイルと 2 つの uilabels からの画像があります (インターネット要求ではなく、シングルトンから)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier = @"MenuCell";
MenuCellViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[MenuCellViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//Singleton
DataModel *m = [DataModel getModel];
switch ([indexPath section]) {
case 0: //Only Header
break;
case 1: //My cells
{
//If footer - set footer background
if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1){
[cell setBackgroundView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"imgFooter.png"]]];
//Else - set normal cell background
} else {
[cell setBackgroundView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"img.png"]]];
}
//Get Facebook image
strForImg = [[NSString alloc] initWithFormat:@"https://graph.facebook.com/%@/picture?type=square",[[[m list] objectAtIndex:indexPath.row] objectForKey:@"id"]];
url =[NSURL URLWithString:strForImg];
img = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
[cell.imgProfile setImage:img];
//Set some text (uilabels)
cell.lblNom.text = [[[m list] objectAtIndex:indexPath.row] objectForKey:@"name"];
cell.lblTour.text = [[[m list] objectAtIndex:indexPath.row] objectForKey:@"description"];
break;
}
}
}
このコードは非常に遅く、facebook から各セルのプロファイル画像を動的にロードするため、テーブルビューがフリーズします。
こんなディスパッチでやってみた
case 1://My cells
{
if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1){
[cell setBackgroundView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"imgFooter.png"]]];
} else {
[cell setBackgroundView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"img.png"]]];
}
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
dispatch_sync(dispatch_get_main_queue(), ^{
strForImg = [[NSString alloc] initWithFormat:@"https://graph.facebook.com/%@/picture?type=square",[[list objectAtIndex:indexPath.row] objectForKey:@"id"]];
url =[NSURL URLWithString:strForImg];
img = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
[cell.imgProfile setImage:img];
});
});
cell.lblNom.text = [[[m list] objectAtIndex:indexPath.row] objectForKey:@"name"];
cell.lblTour.text = [[[m list] objectAtIndex:indexPath.row] objectForKey:@"description"];
break;
}
速度はわずかに増加しましたが、スクロールするとまだフリーズしています。
私の発送は正しくありませんか?(私はそれを発見したばかりです。おそらく、あまり使用されていません)。
または、より良い方法はありますか?Facebookの友達ピッカーが好きです。デフォルトのプロファイル画像を配置し、インターネットから受信したときに正しい画像プロファイルを更新します。この魔法はどのように行われますか?:)