简体   繁体   English

迅速收集缓慢查看

[英]Swift slow collectionView

I have a collection view that takes images I have stored and displays them. 我有一个收集视图,该视图会拍摄我存储的图像并显示它们。 However, when I run it, I get a memory error and a really slow/laggy display. 但是,当我运行它时,会出现内存错误和非常缓慢/缓慢的显示。 Here is my code: 这是我的代码:

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
}

Is there an easier way to do this that I'm missing? 有没有一种更简单的方法可以实现我所缺少的功能?

You should use Instruments to figure out what it is that is slow. 您应该使用Instruments来找出缓慢的原因。 That's what it's for! 这就是它的目的!

However, I'm going to guess that this line is the problem: 但是,我猜这是问题所在:

cell.imageViewTwo.image = UIImage(named: "\(players[indexPath.row])")

If those images are big, two bad things will happen: it takes time to open each one, and you are wasting memory because the displayed image is much smaller than the loaded image. 如果这些图像很大,则会发生两件事:打开每个图像花费时间,并且由于显示的图像比加载的图像小得多,因此浪费了内存。 And this is happening while you scroll . 而这是在您滚动时发生的。 Everything you do in cellForItemAtIndexPath: needs to be very, very fast for exactly this reason. 正是由于这个原因,您在cellForItemAtIndexPath:执行的所有操作都必须非常非常快。 Well, your implementation is clearly not fast. 好吧,您的实现显然不是很快。

You have two choices - actually you could use both: 您有两个选择-实际上您可以同时使用两个:

  • Prepare better images. 准备更好的图像。 Use png and use them at the actual size needed for display. 使用png并以显示所需的实际大小使用它们。

  • Use lazy image loading so that you load the images in a background thread and don't slow down the scrolling. 使用惰性图像加载,以便将图像加载到后台线程中,并且不会减慢滚动速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM