OGeek|极客世界-中国程序员成长平台

标题: ios - UICollectionView 初始滚动滞后? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:15
标题: ios - UICollectionView 初始滚动滞后?

我已经使用 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] sortedArrayUsingSelectorselector(localizedCaseInsensitiveCompare];

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

// Populate each cell with specified data

- (UICollectionViewCell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)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)collectionViewUICollectionView *)colView didHighlightItemAtIndexPathNSIndexPath *)indexPath {

    UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];

    CALayer * l = [cell layer];

    [l setMasksToBounds:YES];

    [l setCornerRadius:10.0];

    cell.contentView.backgroundColor = [UIColor lightGrayColor];

}

- (void)collectionViewUICollectionView *)colView didUnhighlightItemAtIndexPathNSIndexPath *)indexPath 
{
    UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = nil;    
}

这与我在 cellForItemAtIndexPath 中动态设置“imageView”的角半径以及 didHighlightItemAtIndexPath 中单元格本身的角半径这一事实有什么关系吗?

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

谢谢



Best Answer-推荐答案


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

需要考虑的事情

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






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4