1
setNeedsDisplay
を呼び出すために画像を非同期にダウンロードするブロック内で、メインスレッドを表示してより速く表示させる。cellForRowAtIndexPath:メインスレッドで呼び出しても、バックグラウンドでダウンロードされた画像に対してsetNeedsDisplayが機能しない
dispatch_async(main_queue, ^{
[view setNeedsDisplay];
});
私はこれを次のように試していますが、画像はダウンロードされるとすぐに表示されません。一般に、約4〜5秒の遅延があります。特定の行を選択するとイメージが表示され、他の行はまだ表示されないため、これはわかります。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UIImageView *iv = (UIImageView*)[cell viewWithTag:kCellSubViewWavImageView];;
//async for scroll performance
dispatch_queue_t queue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSURL *url = [[NSURL alloc] initWithString:[self.user.favWavformURLAr objectAtIndex:indexPath.row]];
NSLog(@"background");
NSData *imageData = [[NSData alloc] initWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:imageData];
iv.image = image;
dispatch_queue_t main_queue = dispatch_get_main_queue();
dispatch_async(main_queue, ^{
NSLog(@"main thread");
[iv setNeedsDisplay];
});
});
}
return cell;
}
尚、以下NSLog(@"background");
とNSLog(@"main thread");
呼び出しは、私が思うに、私が期待するものである最初の6個の細胞のための呼び出しは、次の順序で印刷されています。しかし、まだ動作しません:
2012-02-17 20:46:27.120 SoundcloudFavs[8836:1c03] background
2012-02-17 20:46:27.169 SoundcloudFavs[8836:1b03] background
2012-02-17 20:46:27.170 SoundcloudFavs[8836:6b07] background
2012-02-17 20:46:27.173 SoundcloudFavs[8836:7503] background
2012-02-17 20:46:27.174 SoundcloudFavs[8836:7103] background
2012-02-17 20:46:27.177 SoundcloudFavs[8836:8003] background
2012-02-17 20:46:27.219 SoundcloudFavs[8836:207] main thread
2012-02-17 20:46:27.270 SoundcloudFavs[8836:207] main thread
2012-02-17 20:46:27.282 SoundcloudFavs[8836:207] main thread
2012-02-17 20:46:27.285 SoundcloudFavs[8836:207] main thread
2012-02-17 20:46:27.296 SoundcloudFavs[8836:207] main thread
2012-02-17 20:46:27.300 SoundcloudFavs[8836:207] main thread
アイデア?
ありがとうございました...それがうまくいった!私はそれを考えなかったとは信じられません。 – Remover