UICollectionView初始滚动滞后?

时间:2015-01-28 19:44:46

标签: ios objective-c uicollectionview uicollectionviewcell

我已使用3x18类设置了UICollectionView“网格视图”。每个单元格都包含从UIImageView加载的NSDictionary。共有18张图片,总数约为“20 MB”。

我遇到的问题是当我最初在视图中时,我从顶部滚动到底部,这是滞后的。之后,滚动就像照片应用程序一样流畅。

为什么会这样?是因为我的图像太大了吗?

这是我的代码:

- (void)viewDidLoad

{
    [super viewDidLoad];

    dictionary = @{@"01"    : @[@"Image 1"],
                   @"02"    : @[@"Image 2"],
                   // etc etc 
                  };

    numOfImages = [ [dictionary allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

    // Allows appropriate methods to be used
    self.pageantCollectionView.delegate = self;
    self.pageantCollectionView.dataSource = self;
}

// Populate each cell with specified data

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

{
    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    NSString *number = [numOfImages objectAtIndex:indexPath.row];

    NSArray *imageByNumber = [dictionary objectForKey:number];

    NSString *name= [imageByNumber objectAtIndex:indexPath.section];

    cell.nameLabel.text = name;

    cell.imageView.image = [UIImage imageNamed:[self getImage:name] ];

    // Set rounded corners
    CALayer * l = [cell.imageView layer];

    [l setMasksToBounds:YES];

    [l setCornerRadius:10.0];

    return cell;
}

- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {

    UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];

    CALayer * l = [cell layer];

    [l setMasksToBounds:YES];

    [l setCornerRadius:10.0];

    cell.contentView.backgroundColor = [UIColor lightGrayColor];

}

- (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath 
{
    UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = nil;    
}

是否与我在cellForItemAtIndexPath内动态设置“imageView”的转角半径,以及didHighlightItemAtIndexPath内单元格的转角半径有什么关系?< / p>

如果是这种情况,我该如何静态创建“imageView”和“cell”本身的角半径?

由于

2 个答案:

答案 0 :(得分:2)

创建自定义UICollectionViewCell,并在awakeFromNib方法的单元格的子类中,设置角半径和其他任何内容。这是为了防止它每次都修改图层(就像目前通过cellForItem方法一样)。

即:

CustomCell *cell = (CustomCell*)[colView cellForItemAtIndexPath:indexPath];

还要确保在viewDidLoad中注册单元格(您可以找到全部的自定义单元格指南)。

答案 1 :(得分:2)

延迟更可能是使用[UIImage imageNamed:]加载图像。第一次,这些是从磁盘加载(慢)。随后它们将从内存缓存中加载,这将很快。你会得到更糟糕的结果,你的图像越大。

需要考虑的事项

  • 与图片尺寸相比,您的图片(像素尺寸)有多大?如果它们大得多,则应考虑以显示尺寸提供缩略图版本。

  • 预压。是否有更好的地方在应用程序中引发这种延迟?例如在启动期间(在显示collectionView之前?如果是这样的话,尝试将它们全部(使用imageNamed :)加载到NSDictionaryNSCache(这就像是一个被刷新的词典内存不足的情况)

  • 异步加载。请考虑使用UIImage imageFromContentsOfFile:,您可以将其放入后台线程(然后将图像加载到主线程上的imageView中)。与imageNamed不同,此方法不会缓存,但您可以通过将结果添加到NSCache来手动执行此操作。您需要编写一些代码,以便在可用时从缓存中获取图像,而不是从磁盘中重新获取图像。
    可能可以使用imageNamed执行此操作 - 但它不是线程安全的,因此您不应该真正尝试它(请参阅this answer旁边的注释)。

  • 可能在主线程上显示图像之前强制在后台线程上进行初始屏幕外绘制。这会强制图像在后台解压缩。您需要结合之前的技术尝试这种技术,看看它是否真的对您有所帮助。

  • 细胞初始化。任何只需要发生一次到单元格的东西,都不应该在cellForItem...中完成。你可以将这些东西(例如细胞层角半径)移动到单元init方法中,正如Daniel Shaffer所建议的那样。无论如何你应该这样做,虽然我不认为这将是你当前滚动困境的主要原因。

相关问题