I have a UIImageView
and I want to append there images coming from a remote URL
Up to now it works fine with only one image.
I just set cell.related.image
to the new image and I return always 1 to the collectionView
How can I switch to appending an array of images?
Is there a way to do the appending in a function?
Do I need always to do the appending inside collectionView
?
I want to do it inside an event listener in the future
var img_uls = ["url1" , "url2", "url3"]
func append_image(_ path: String, _ cell: RelatedCollectionViewCell) {
let url = URL(string: path)
DispatchQueue.global().async {
let data = try? Data(contentsOf: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
DispatchQueue.main.async {
cell.related.image = UIImage(data: data!)
}
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return img_uls.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RelatedCollectionViewCell", for: indexPath) as! RelatedCollectionViewCell
append_image(img_uls[indexPath.row], cell);
return cell
}