异步图片加载闪烁?

问题描述 投票:0回答:1

我尝试了很多异步加载图像的方法,但它们都有这个问题,包括第三方框架。这让我相信这与我的单元格行索引方法有关。

无论如何,我目前正在使用 Grand Central Dispatch 和 Blocks 异步加载图像,这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CustomTableCell";
    static NSString *CellNib = @"CustomizedCell";

    CustomizedCell *cell = (CustomizedCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
        cell = (CustomizedCell *)[nib objectAtIndex:0];

    }



    // Configure the cell...

    cell.Label.text = [self.TitleArray objectAtIndex:indexPath.row];    

       dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
       dispatch_async(queue, ^{
           NSString *String = [[self.Array objectAtIndex:indexPath.row];
           NSURL *url = [NSURL URLWithString:String];
           NSData *Data = [NSData dataWithContentsOfURL:url];
           UIImage *Image = [UIImage imageWithData:Data];
           dispatch_sync(dispatch_get_main_queue(), ^{
               cell.ImageView.image = Image;
           });
       });

这似乎是异步加载图像,但就像我所有使用第三方库、不同代码等的尝试一样,图像会闪烁。我的意思是,当您滚动一个单元格的图像时,它似乎会闪烁并更改为另一个单元格的另一个图像,然后快速变回来,使其看起来好像在闪烁。仅当您滚动时才会发生这种情况,并且通常仅滚动到顶部两个和底部两个单元格。 有什么想法吗? 如果有人可以帮助我解决这个问题,我将非常感激!

谢谢!

iphone asynchronous tableview grand-central-dispatch flicker
1个回答
2
投票

好吧,暂时不要使用 GCD,但我发现你的代码有问题。

您是否随时取消当前流程?我的意思是,您滚动查看其他单元格,但已经有一个进程下载(旧的和可重复使用的)单元格的图像,不是吗?也许这就是你的“闪烁”问题。

如果我是你,我会创建 UIImageView 的子类,并实现一种在要加载另一个图像时取消当前连接的方法。

顺便说一句,您可以使用不使用任何第三方库的 NSOperation 和 NSOperationQueue。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.