iphone - 异步图片加载闪烁?

标签 iphone asynchronous tableview grand-central-dispatch flicker

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

无论如何,我目前正在使用 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;
           });
       });

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

谢谢!

最佳答案

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

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

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

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

祝你好运。

关于iphone - 异步图片加载闪烁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8026295/

相关文章:

java - 程序在 Mongodb 异步查询完成之前终止

c++ - 异步使用线程池?

c# - 使用任务处理异步方法异常的问题

iphone - 我如何使一个表格 View 指向具有另一个表格 View 的页面?

iphone - iphone 应用程序中的 html 页面/常见问题解答

iphone - 当您在移动设备上单击目标 ="_blank"链接时会发生什么?

JavaFX : How to populate data to TableView from different Thread(Netty)

ios - 如何为表格 View 部分中标题的高度变化设置动画?

iphone - 测试App Store "Distribution"版本

ios - WinObjC 的 AFNetworking