
[英]How to create a slow and continuous scroll with CollectionView Swift
[英]Swift slow collectionView
我有一个收集视图,该视图会拍摄我存储的图像并显示它们。 但是,当我运行它时,会出现内存错误和非常缓慢/缓慢的显示。 这是我的代码:
var players = ["1", "2", "3", "4", "5", "10", "11", "12", "13", "15", "20", "22", "24", "25", "31", "31", "33", "34"]
@IBOutlet weak var collectionView: UICollectionView!
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 17
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as HomeCollectionViewCell
cell.imageViewTwo.image = UIImage(named: "\(players[indexPath.row])")
return cell
}
有没有一种更简单的方法可以实现我所缺少的功能?
您应该使用Instruments来找出缓慢的原因。 这就是它的目的!
但是,我猜这是问题所在:
cell.imageViewTwo.image = UIImage(named: "\(players[indexPath.row])")
如果这些图像很大,则会发生两件事:打开每个图像花费时间,并且由于显示的图像比加载的图像小得多,因此浪费了内存。 而这是在您滚动时发生的。 正是由于这个原因,您在cellForItemAtIndexPath:
执行的所有操作都必须非常非常快。 好吧,您的实现显然不是很快。
您有两个选择-实际上您可以同时使用两个:
准备更好的图像。 使用png
并以显示所需的实际大小使用它们。
使用惰性图像加载,以便将图像加载到后台线程中,并且不会减慢滚动速度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.