Reputation: 83
I have this array:
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")!]
]
Also I have a 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
}
But I have a problem. When I scroll my collectionView
my app freezing. But when I scroll for all section and continue scroll next. My app not freezing.
How to fix it?
Upvotes: 1
Views: 1480
Reputation: 7669
For better image loading on TableView/CollectionView cell SDWebImage is perfect image loading library. Add this to your project and use for loading image on collectionView. It will remove the UI freezing issue during image load.
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
}
Hope this will help.
Upvotes: 1
Reputation: 2916
Try saving only the name of the images in your array like
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
}
Try and share the result.
Upvotes: 0
Reputation: 1421
Please check whether your 'media' array is in sectioned or not. As you have shown how you have allocated your 'media' array, is just a single image array. Also, you are trying to get image name from a sectioned array which is the cause of the crash.
So, better to use 'media' array directly without any section or if in case you want to access it as a sectioned array, then allocate 'media' array as a sectioned array.
Upvotes: 0