I've got a problem with my images. The following code works in a tableViewController but doesn't work in my CollectionView.
this works...
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = listOfVideos[indexPath.row]?.title
cell.detailTextLabel?.text = listOfVideos[indexPath.row]?.duration
let imgUrl = listOfVideos[indexPath.row]?.videoImgUrl
do {
let bindData = try Data(contentsOf: imgUrl!)
cell.imageView?.image = UIImage(data: bindData)
} catch {
print(error.localizedDescription)
}
return cell
}
this doesn't work
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ImageCollectionViewCell
let imgUrl = listOfImages[indexPath.row]?.imagePreviewUrl
do {
let bindData = try Data(contentsOf: imgUrl!)
cell.img.image = UIImage(data: bindData)
} catch {
print(error.localizedDescription)
}
cell.backgroundColor = UIColor.blue
return cell
}
The url is valid and the image exists. The error "fatal error unexpectedly found nil while unwrapping an Optional value" occurred when UIImage(data: bindData) is called.
Thanks Tom