Alamofire没有使用网址下载图片

时间:2017-02-08 00:45:49

标签: swift alamofire

TableView默认功能:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let post = posts[(indexPath as NSIndexPath).row]

    if let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as? PostCell {

        cell.request?.cancel()
        var img: UIImage?

        if let url = post.imageUrl {
            img = FeedVC.imageCache.object(forKey: url as AnyObject) as? UIImage
        }

        cell.configureCell(post, img: img)

        return cell
    } else {
        return PostCell()
    }
}

配置单元格功能:

func configureCell(movie: Movie, img: UIImage?){

    self.movieTitle.text = movie.title
    self.movieCaption.text = movie.caption

    if movie.imgURL != nil {

        if img != nil {
            self.movieImg.image = img
        }
        else {

            let url = movie.imgURL

            request = Alamofire.request(url!).response { response in
                let img = UIImage(data: response.data!)! //always returns with "fatal error: unexpectedly found nil while unwrapping an Optional value"

                self.movieImg.image = img
                ViewController.imageCache.setObject(img, forKey: url as AnyObject)
            }

        }
    }
    else {
        self.movieImg.isHidden = true
    }


}

出于某种原因,每当我运行此代码时,它总是崩溃,输出为:

  

致命错误:在解包可选值时意外发现nil。

当我将图像设置为UIImage(data)时,这是在配置单元格功能上。我尝试了很多场景,但未能弄清楚我的问题是什么。

0 个答案:

没有答案