I'm trying to add an image from URL to UIImageView
. I get no errors but nothing is showing on the screen.
What am I doing wrong here?
I used the same approach with images from .xcassets
and it worked fine, but it's not working from a remote url.
Here are my files:
RelatedCollectionViewCell.swift
class RelatedCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var related: UIImageView!
}
ViewController.swift
var images = [AnyObject]()
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
do {
try images.append(UIImage(data: Data(contentsOf: URL(string:"https://i.ytimg.com/vi/kWxHV9jkwSA/hqdefault.jpg")!))!)
} catch {
print(error)
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RelatedCollectionViewCell", for: indexPath) as! RelatedCollectionViewCell
cell.related.image = images[indexPath.row] as? UIImage
return cell
}