简体   繁体   English

带有NINetworkImageViews的UICollectionView重复图像

[英]UICollectionView with NINetworkImageViews repeats images

I'm using NINetworkImageView from Nimbuskit. 我正在使用Nimbuskit的NINetworkImageView。 I have a simple UICollectionView showing a grid of images (NINetworkImageViews). 我有一个简单的UICollectionView,它显示图像网格(NINetworkImageViews)。 Normally it works like a charm, but in certain situations some of the NINetworkImageViews show a wrong image. 通常,它的工作原理像一个超级按钮,但是在某些情况下,某些NINetworkImageViews显示错误的图像。

I think it happens when there is no path for the image and NINetworkImageView has to show the default image. 我认为当图像没有路径并且NINetworkImageView必须显示默认图像时会发生这种情况。 Sometimes (few times) instead of showing the default image, it appears other image belonging to other NINetworkImageView of the CollectionView. 有时(几次)它没有显示默认图像,而是显示了其他图像,这些图像属于CollectionView的其他NINetworkImageView。

Here is the related code: 以下是相关代码:

- (UICollectionViewCell*) collectionView:(UICollectionView *)collectionView   cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"ShelvingCell";
    ShelvingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    // Prueba para ver si esto arregla lo de que se repitan imágenes
    [cell.feedNetworkImageView prepareForReuse];

    cell.feedNetworkImageView.delegate = self;

    // El collage necesita un tratamiento especial
    if (indexPath.section == 0 && indexPath.row == 0)
    {
        cell.feedTitleLabel.text = @"";
        cell.lastFeedNewTitleLabel.text = @"";
        // Prueba para ver si esto arregla lo de que se repitan imágenes
        [cell.feedNetworkImageView setPathToNetworkImage:@""];

        // Prueba para ver si esto arregla lo de que se repitan imágenes
        [cell.feedNetworkImageView prepareForReuse];

        cell.feedNetworkImageView.image = [UIImage imageNamed:@"collage.png"];
    }
    else
    {    
        MOFeed *feed = [self.feeds objectAtIndex:(indexPath.section*3 + indexPath.row - 1)];
        cell.feedTitleLabel.text = feed.title;

        // Recuperamos la última noticia del feed
        MONoticia *ultimaNoticia = [feed lastFeedNew];

        cell.lastFeedNewTitleLabel.text = ultimaNoticia.title;

        if (![feed.feedImageURL isEqualToString:@""] && feed.feedImageURL)
        {
            // Prueba para ver si esto arregla lo de que se repitan imágenes
            [cell.feedNetworkImageView prepareForReuse];
            [cell.feedNetworkImageView setPathToNetworkImage:feed.feedImageURL];
        }
        else
        {
            // Prueba para ver si esto arregla lo de que se repitan imágenes
            [cell.feedNetworkImageView prepareForReuse];

            cell.feedNetworkImageView.image = [UIImage imageNamed:@"shelvingcell.png"];
            // Prueba para ver si esto arregla lo de que se repitan imágenes
            [cell.feedNetworkImageView setPathToNetworkImage:@""];
        }
    }
    return cell;
}

Thanks a lot! 非常感谢!

Carlos 卡洛斯

I had the same problem, and when i dig into the issue, it looks like 我遇到了同样的问题,当我深入研究问题时,看起来

[cell.feedNetworkImageView prepareForReuse];

didn't cancel all download operation. 没有取消所有下载操作。 you have to do a little hack 你必须做一点hack

before call 通话前

[cell.feedNetworkImageView setPathToNetworkImage:feed.feedImageURL];

cancel all network operation operation and then create a new network operation 取消所有网络操作操作,然后创建一个新的网络操作

[self.displayImage.networkOperationQueue cancelAllOperations];
self.displayImage.networkOperationQueue = [[NSOperationQueue alloc]init];
[cell.feedNetworkImageView setPathToNetworkImage:feed.feedImageURL];

it's not as efficient since you each image download need it's own operation queue so it can be cancelled. 由于您每次下载映像都需要它自己的操作队列,因此可以取消它,因此效率不高。 this may need a little bit more work to make it more efficient. 这可能需要更多的工作才能提高效率。 but so far it looks like there is no repeated images 但到目前为止,似乎没有重复的图像

Carlos, I don't know much about NINetworkImageView, but my theory is that UICollectionView is reusing your views, and you don't cancel the request. 卡洛斯,我对NINetworkImageView不太了解,但是我的理论是UICollectionView正在重用您的视图,并且您不会取消请求。 Say you load the first cell, and then a request is being triggered for that image. 假设您加载了第一个单元格,然后对该图像触发了一个请求。 Then you start scrolling. 然后,您开始滚动。 That first cell is reused before the first request finishes, and probably this new item reusing that cell does not have an image, so you don't call setPathNetwork... The result, the new reused cell will have the first cell image. 该第一个单元将在第一个请求完成之前被重用,并且可能这个重用该单元的新项没有图像,因此您不必调用setPathNetwork ...结果,新的重用单元将具有第一个单元图像。

Probably there is a method in NINetworkImageView to cancel the request. NINetworkImageView中可能存在一种取消请求的方法。 Call it everytime you reuse one of these views. 每次重用这些视图之一时调用它。

Un saludo! sal!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM