ios - 在 Swift 中加载图像时应用程序冻结
问题描述
我有这个数组:
media = [
[UIImage(named: "1.png")!,UIImage(named: "14.png")!,UIImage(named: "2.png")!],
[UIImage(named: "3.png")!,UIImage(named: "15.png")!,UIImage(named: "4.png")!],
[UIImage(named: "5.png")!,UIImage(named: "16.png")!,UIImage(named: "6.png")!],
[UIImage(named: "7.png")!,UIImage(named: "17.png")!,UIImage(named: "8.png")!],
[UIImage(named: "9.png")!,UIImage(named: "18.png")!,UIImage(named: "10.png")!],
[UIImage(named: "11.png")!,UIImage(named: "19.png")!,UIImage(named: "12.png")!],
[UIImage(named: "13.png")!,UIImage(named: "21.png")!,UIImage(named: "47.png")!]
]
我还有一个collectionView
:
func numberOfSections(in collectionView: UICollectionView) -> Int {
return media.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return media[section].count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MasterViewCell
cell.thisImage.image = media[indexPath.section][indexPath.row]
return cell
}
但我有一个问题。当我滚动我collectionView
的应用程序时冻结。但是当我滚动所有部分并继续滚动下一步时。我的应用程序没有冻结。
如何解决?
解决方案
为了更好地在 TableView/CollectionView 单元格上加载图像, SDWebImage是完美的图像加载库。将此添加到您的项目中并用于在 collectionView 上加载图像。它将消除图像加载期间的 UI 冻结问题。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MasterViewCell
cell.thisImage.sd_setImage(with:Bundle.main.url(forResource: "\(indexPath.row + 1)", withExtension:"png"), placeholderImage: UIImage(named: "placeholder.png"))
return cell
}
希望这会有所帮助。
推荐阅读
- java - 使用键在活动之间传递值
- c++ - 推导显式特化的函数模板类型
- javascript - Vue - 尝试将值作为数字发出并在发出时获取字符串
- jquery - 在其他 div 的滚动上修复容器 div
- javascript - Angular 8 NgRx - 错误:检测到不可序列化的操作
- javascript - 将 localStorage 输入数据传递到下一页
- rapids - 替换 C 列中的值,其中 A 列中的值为 x
- javascript - 从对象中的其他数组创建数组
- sql - 使用 SUM 作为检索表之一时,如何保留 LEFT OUTER JOIN 表中的所有值,为什么 GROUP BY 似乎可以解决问题?
- php - 如何编写将子范围转换为索引对的 A 到 Z 词索引算法?