来自Web的UITableViewCell图像:在展开Optional值时意外发现致命错误为零

时间:2017-05-22 20:50:27

标签: ios uitableview swift3 uiimage

我的图片出现问题。以下代码在tableViewController中工作,但在我的CollectionView中不起作用。

这有效......

    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
}

这不起作用

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
}

网址有效且图片存在。调用UIImage(data:bindData)时发生错误“在解包可选值时意外发现致命错误nil”。

谢谢汤姆

0 个答案:

没有答案