I am using this code to display image in to my customCell. This is my code to fetch image from API. It was working fine as per the given link. But the problem started when I added this code to my cellForItemAt indexPath:
for image Cache.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// Displaying other elements with text
customCell.mainImage.image = nil
var image1 = self.imageCache.object(forKey: indexPath.item as AnyObject) as? UIImage
if (image1 == nil) {
self.ImageFetcher(postId: self.myArray[indexPath.item].id!, completion: { (image) in
image1 = image
self.imageCache.setObject(image1!, forKey: indexPath.item as AnyObject)
})
}
DispatchQueue.main.async {
customCell.mainImage.image = image1
}
}
Above code works fine without any error but, not displaying/loading images until I scroll the collectionView up and down. Don't understand what I am missing here. Any help would be appreciated !!
Please try this,