【问题标题】:UICollectionView Scroll Freeze with NSData (imageView)UICollectionView 滚动冻结与 NSData (imageView)
【发布时间】:2015-02-13 22:59:18
【问题描述】:

我在同一页面中有 UICollectionView 和 UITableView。我正在使用SDWebImageUITableView,它工作正常。我正在尝试将SDWebImageUICollectionView 一起使用,但无法成功。所以我使用了 NSData 但它冻结了。我该如何解决这个问题?

更新:有没有办法将SDWebImageUICollectionView 一起使用?

我的代码:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

static NSString *simpleTableIdentifier = @"cell";

ProductsCollectionViewCell *pCell = [_collection dequeueReusableCellWithReuseIdentifier:simpleTableIdentifier forIndexPath:indexPath];

NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[_arrayImage objectAtIndex:indexPath.row]]];

_imgCollection = (UIImageView *)[pCell viewWithTag:100];
_imgCollection.image = [UIImage imageWithData:imageData];

pCell.layer.shouldRasterize = YES;
pCell.layer.rasterizationScale = [UIScreen mainScreen].scale;

}

我的表格视图代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"productCell";

ProductsTableViewCell *pCell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

[pCell.imgProduct sd_setImageWithURL:[NSURL URLWithString:[_arrayImage objectAtIndex:indexPath.row]]
                    placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

【问题讨论】:

    标签: ios uicollectionview nsdata uicollectionviewcell sdwebimage


    【解决方案1】:

    你使用的方法

    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[_arrayImage objectAtIndex:indexPath.row]]];
    

    是同步的,这意味着它将阻塞线程直到它完成。 (这就是它冻结的原因)

    我建议你想办法在你的代码中使用SDWebImage,如果你告诉我们为什么这不起作用,我们可能会帮助你。

    编辑:

    SDWebImagedocumentation中的第一个例子,特别是这一行:

    [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                       placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    

    现在用它代替这行代码:

    _imgCollection.image = [UIImage imageWithData:imageData];
    

    此外,删除下载图像的行。

    【讨论】:

    • 我不知道如何将它与collectionview 链接起来,就像在tableviewcell 中一样。我需要知道它的写法
    • 我编辑了我的答案以帮助您将SDWebImageUICollectionView 一起使用
    • 我已使用您的代码并对其进行了编辑,它运行良好。
    【解决方案2】:

    难道是这样的

    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL ...
    

    是否在主线程上执行耗时操作?

    为什么不尝试像这样将 fetch 放在后台?

    dispatch_async(
             dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSURL* url = [NSURL URLWithString:[_arrayImage objectAtIndex:indexPath.row]]
        NSData *imageData = [NSData dataWithContentsOfURL:url];
        dispatch_async(dispatch_get_main_queue(), ^{
              UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
              if(cell) { // cell is visible
                 cell.imageView.image = [UIImage imageWithData:imageData];
              }
    
        });
    });
    

    最后,确保

    [_arrayImage objectAtIndex:indexPath.row] 
    

    确实是一个字符串,而 _arrayImage 是一个字符串数组

    最后我会考虑放

    [UIImage imageWithData:imageData]
    

    在后台以及解码也可能很耗时

    【讨论】:

    • 我试过这种方式,它不会冻结,但它不会填满所有单元格,并且在同一时刻所有图像都在不断变化。
    • 是的,其中一些保持空白。我所说的连续变化的意思是,例如,我有 10 个图像在同一个单元格中每 0.5 秒都非常快地变化。
    • 原因可能是因为当您滚动时,您的单元格在图像加载之前消失了。由于单元格总是被重用(在表格和集合视图中),异步块可能在单元格被删除后仍然运行,并在图像被出列(再次使用)时将其放入不同的 indexPath 中。然后,新块在同一个单元格上运行,您会看到图像的快速变化。我已经修改了上面的代码以捕获索引路径并使用它来尝试将图像放置在正确的单元格中。这有意义吗?
    【解决方案3】:

    可能是你缺少 contentView

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *simpleTableIdentifier = @"cell";
    
    ProductsCollectionViewCell *pCell = [_collection dequeueReusableCellWithReuseIdentifier:simpleTableIdentifier forIndexPath:indexPath];
    
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[_arrayImage objectAtIndex:indexPath.row]]];
    
    _imgCollection = (UIImageView *)[pCell.contentView viewWithTag:100];
    _imgCollection.image = [UIImage imageWithData:imageData];
    }
    

    【讨论】:

    • 我添加了 contentView 但它仍然冻结。
    • 我可以看到你有子类 collectionviewcell 为什么不在界面生成器中链接图像视图字段并在此处访问。
    • 我认为 pCell 可能为零
    • 我已经尝试过接口构建器的子类和直接链接,但没有什么不同。
    • 检查你的 ImageData 这可能是 nil
    猜你喜欢
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    • 2016-12-30
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多