ios - UICollectionView 初始滚动滞后?

标签 ios objective-c uicollectionview uicollectionviewcell

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

我遇到的问题是,当我最初在 View 中时,我从顶部滚动到底部,它很慢。之后,滚动就像照片应用一样流畅。

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

这是我的代码:

- (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 中单元格本身的角半径这一事实有什么关系吗?

如果是这样,我该如何静态地为“imageView”和“cell”本身创建圆角半径?

谢谢

最佳答案

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

需要考虑的事情

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

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

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

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

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

关于ios - UICollectionView 初始滚动滞后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28201019/

相关文章:

objective-c - 是否可以提交给Mac App Store(OsX Mavericks除外)进行下载

iphone - 使用 NSPredicate 过滤 NSArray

objective-c - NSFetchedResultsController 不调用 controllerDidChangeContent : after update to non-fetched NSManagedObject

iOS UICollectionView 推送刷新时出现意外行为

IOS - SWIFT - 带有图像的 CollectionView,从数组加载 URL

iphone - 在 PC 上使用 iPhone 摄像头作为网络摄像头

ios - 如何在 Swift 中重新加载节标题而不使其消失/重新出现?

IOS:如何在 Swift 中增加 AVAudioPlayer 的低音和高音?

objective-c - Objective C 协议(protocol)的使用

swift - 当我使用按钮增加单元格高度时,UICollectionView 单元格彼此重叠