- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我已经使用 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:) 到 NSDictionary
或 NSCache
(这就像一个被刷新的字典在内存不足的情况下)
异步加载。考虑改用 UIImage imageFromContentsOfFile:
,您可以将其放入后台线程(然后将图像加载到主线程的 imageView 中)。与 imageNamed
不同,此方法不缓存,但您可以通过将结果添加到 NSCache
来手动执行此操作。您将需要编写一些代码,以便在图像可用时从缓存中获取图像,而不是从磁盘重新获取图像。
您可能可以使用 imageNamed
执行此操作 - 但它不是线程安全的,因此您不应该真正尝试它(请参阅 this answer 旁边的评论)。
可能 在主线程上显示图像之前强制在后台线程上进行初始屏幕外绘制。这会强制图像在后台解压缩。您需要将此技术与之前的技术结合起来进行试验,看看它是否真的对您有所帮助。
单元初始化。任何只需要对一个单元格发生一次的事情,都不应该在 cellForItem...
中完成。正如 Daniel Shaffer 所建议的,您可以将这些东西(例如您的单元格层角半径)移动到单元格 init
方法中。无论如何你都应该这样做,虽然我不认为这将是你当前滚动问题的主要原因。
关于ios - UICollectionView 初始滚动滞后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28201019/
我有一个并排有两列的布局。有没有一种简单的方法可以使用单个 UICollectionView 来做到这一点?唯一的要求是该解决方案必须适用于 iOS 8,并且单元格必须在每列中垂直堆叠,如下所示:
我使用标准 UICollectionView与部分。我的单元格像网格一样排列。如果用户使用 Apple TV Remote 移动焦点,则所选方向的下一个单元格会正确聚焦。但是如果网格中有“间隙”,默认
这是我的 ImageController 类(一个 ViewController)仅显示 Collection View 的背景,而其中的单元格则不显示。有什么帮助吗?是不是我没有正确初始化什么? 这
我希望能够在 UICollectionView 中设置内容大小的最小高度,这样我就可以隐藏/显示 UISearchbar,类似于在 iBooks 上完成的方式。 但是,我不想对布局进行子类化,因为我想
有没有办法禁用 UICollectionView 的自动滚动细胞何时聚焦?当单元格进入焦点时,我想手动调整单元格的内容偏移量。 我不想更新内容偏移量: - (void)didUpdateFocusIn
如何将 UICollectionView 中的所有单元格垂直和水平居中使用 UICollectionViewFlowLayout ? 流布局根据滚动方向在右侧或底部留下间距。我想在所有方面设置相同的填
所以这是一个很无聊的问题,但我只是想不出一种非常简单的方法来检测当前关注的项目的 indexPath . 我环顾四周,希望看到一些非常简单的东西,例如 collectionView.indexPath
每当 UICollectionView 完全加载时,我都必须执行一些操作,即此时应调用所有 UICollectionView 的数据源/布局方法。我怎么知道?是否有任何委托(delegate)方法可以
每当 UICollectionView 完全加载时,我都必须执行一些操作,即此时应调用所有 UICollectionView 的数据源/布局方法。我怎么知道?是否有任何委托(delegate)方法可以
所以,UICollectionView 现在对我来说真的很痛苦。假设我有一个 UIViewController ,其中嵌入了一个 UICollectionView 。 CollectionView 的
好的,如果我有 UIViewController如果我使用这个 `preferredFocusedView ,我可以制作一个专注于 tvOS 的 View ,但是如何让 UICollectionVie
我已经构建了 2 个 Collection View ,1 个水平滚动(在顶部)和 1 个垂直滚动(在中间 - 底部),用于在我的 Objective-C iOS 应用程序中查看 2 组不同的内容,类
这就是我的问题,您在 gif 上看到的 4 个橙色矩形是单个垂直 UICollectionView = OrangeCollectionView。 绿色和紫色“卡片” View 是另一个 UIColl
大家好,我是 IOS 开发的新手,我有一个 UICollection,我在其中使用 SDWebImageCache 加载图像。问题是我的 UICollectionview 单元格在快速滚动时感觉像抽搐
我在 ViewController 中有两个 Collection View 。需要在用户滚动底部 UICollectionView 时自动滚动顶部 collectionView。 最佳答案 你可以试
我正在研究 UICollectionView,我有一个在单元格中使用 collectionView 的想法?可以吗 当我运行项目时,我收到一个错误 "InterFace Builder StoryBo
大家好,我收到以下错误 -UICollectionView 必须使用非 nil 布局参数进行初始化 PopularShotsCollectionViewController 的代码: import U
我的应用程序出现此错误: *** -[UICollectionView _endItemAnimations] 断言失败,/SourceCache/UIKit/UIKit-2372/UICollect
当我的 UIViewController 与 UICollectionView 一起出现时,内容会向上滚动一点,出现时。 我实现了 scrollViewDidScroll:我正在记录 contentO
我遇到了一个问题,在我的 UICollectionView 中,当返回的记录为 0 时,我需要显示一个页脚。下面的代码似乎运行良好,当没有记录时,显示页脚。但是,有一个问题,当有记录时,虽然foote
我是一名优秀的程序员,十分优秀!