0

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

Dima
  • 23,484
  • 6
  • 56
  • 83
  • is your cell.img a valid imageView ? Have you tried to print your bindData ? – GIJOW May 22 '17 at 21:05
  • I think so. The content of data: `Printing description of bindData: ▿ 56894 bytes - count : 56894 ▿ pointer : 0x00007ffed599b810 - pointerValue : 140732482041872 ` The imageView comes from an UIImageViewCell wich is registered by `self.collectionView!.register(ImageCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)` – Thomas Sonntag May 22 '17 at 21:26
  • To help you out, try moving UIImage(data: bindData) one line above like: let img = UIImage(data: bindData) next line cell.img.image = img, then you can see where it is stopping. It will not solve your problem but at least will help you isolating the problem. – GIJOW May 22 '17 at 21:32
  • sometimes it's hard to find when the error occurs. But I tried your way, now it's a little clearer. It stopped at the last line. cell.img... and in Preview I can see the image. But when I let print the Data it says: `Printing description of img: expression produced error: error: /var/folders/t1/9c481wfn4xd6v89j37n6gzjr0000gn/T/./lldb/19631/expr1.swift:1:80: error: use of undeclared type 'UIKit' Swift._DebuggerSupport.stringForPrintObject(Swift.UnsafePointer>(bitPattern: 0x11d302710)!.pointee)` – Thomas Sonntag May 22 '17 at 21:43
  • It seems to be the cell. If I add an ImageView outside the cell. It works. So, now I don't know to hand the cell.. – Thomas Sonntag May 22 '17 at 21:49
  • `cell.img` is almost certainly `nil` Have you linked the `IBOutlet` to the image view in your storyboard? – Paulw11 May 22 '17 at 22:23
  • yes it's linked in the ViewCell. – Thomas Sonntag May 22 '17 at 22:57
  • Okay, my first problem is solved. I'll ask a new question for my other problems. Thanks so far – Thomas Sonntag May 22 '17 at 23:15

0 Answers0