1

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

何か案は?

4

1 に答える 1

3

ダウンロードしたイメージをメインスレッドに設定してみてください。

dispatch_async(main_queue, ^{
            NSLog(@"main thread");
            iv.image = image;
        });

また、セルではなくサブビューとして画像を追加することをお勧めしcell.contentViewます。

于 2012-02-17T21:25:17.960 に答える