我有这个数组:
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
时,我的应用程序冻结了。但是,当我滚动查看所有部分并继续滚动下一个时。我的应用程序没有冻结。
如何解决?
答案 0 :(得分:1)
要在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
}
希望这会有所帮助。
答案 1 :(得分:0)
尝试仅将数组中的图像名称保存为
media = [
["1.png","14.png","2.png"],
["3.png","15.png","4.png"],
// Rest of the image names here.
]
]
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MasterViewCell
let imageName = media[indexPath.section][indexPath.row]
//cell.thisImage.image = UIImage(named: imageName)
cell.thisImage.image = UIImage(named: imageName, in: nil, compatibleWith: nil)
return cell
}
尝试并分享结果。
答案 2 :(得分:0)
请检查“媒体”阵列是否在分区中。如您所展示的,您如何分配“媒体”阵列,仅仅是一个图像阵列。另外,您正在尝试从分段数组中获取映像名称,这是崩溃的原因。
因此,最好直接使用“媒体”数组而不使用任何节,或者如果要以分段数组访问它,则将“媒体”数组分配为分段数组。