swift - 使用 AlamoFireImage 在 CollectionView 中使用 .af_setImage 方法加载数组的 url 时发生 fatal error

标签 swift uicollectionview uicollectionviewcell alamofireimage

我从 REST API 获取了一个 url 数组,我想使用它们为我的 Collection View 单元格使用 AlamofireImage .af_setImage 方法从服务器加载图像。但我在调试控制台中收到以下错误消息:

fatal error: unexpectedly found nil while unwrapping an Optional value

这是我正在使用的代码:

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CollectionViewCellIdentifiers.searchResultCell, for: indexPath) as! CreaTuCanastaCollectionViewCell

        let urlString = productPictures[indexPath.item]

        if let url = URL(string: productPictures[indexPath.item]) {
            cell.imageView.af_setImage(withURL: url)
        }

     return cell
   }
}

奇怪的是调试控制台抛出这个:

Console output

这意味着值(value)观就在那里,但它一直让我困惑 fatal error :

unexpectedly found nil while unwrapping an Optional value

有什么想法吗?

编辑

我尝试使用 SDImage 而不是 AlamoFireImage

 if let url = URL(string: productPictures[indexPath.item]) {

            cell.imageView.sd_setImage(with: url)
        }

我得到了相同的结果

编辑

这次我尝试了不同的方法,我将此代码放入 cellForItemAt 方法中:

Alamofire.request(urlArray).responseImage { response in
            debugPrint(response)

            print(response.request)
            print(response.response)
            print(response.result)

            if let image = response.result.value {

                    cell.imageView.image = image
            }

这在调试控制台中给了我这样的响应:

SUCCESS
<UIImage: 0x620000283fc0>, {300, 300}
[Response]: <NSHTTPURLResponse: 0x62800002cb80> { URL: https://i1.wp.com/pixan.wpengine.com/wp-content/uploads/2016/07/canasta-pareja.jpg?fit=600%2C600&ssl=1 } { status code: 200, headers {
"Cache-Control" = "public, max-age=63115200";
"Content-Length" = 24757;
"Content-Type" = "image/jpeg";
Date = "Thu, 01 Dec 2016 06:06:41 GMT";
Etag = "\"629f656831de2958\"";
Expires = "Sat, 01 Dec 2018 18:00:39 GMT";
"Last-Modified" = "Thu, 01 Dec 2016 06:00:39 GMT";
Link = "<https://pixan.wpengine.com/wp-content/uploads/2016/07/canasta-pareja.jpg>; rel=\"canonical\"";
Server = nginx;
Vary = Accept;
"x-bytes-saved" = 2467;
"x-content-type-options" = nosniff;
"x-nc" = "HIT bur 66";
} }
[Data]: 24757 bytes
[Result]: SUCCESS: <UIImage: 0x6280002830c0>, {300, 300}
[Timeline]: Timeline: { "Request Start Time": 502265200.175, "Initial     Response Time": 502265200.756, "Request Completed Time": 502265200.813, "Serialization Completed Time": 502265200.821, "Latency": 0.581 secs, "Request Duration": 0.638 secs, "Serialization Duration": 0.008 secs, "Total Duration": 0.645 secs }
Optional(https://i1.wp.com/pixan.wpengine.com/wp-content/uploads/2016/07/canasta-pareja.jpg?fit=600%2C600&ssl=1)
Optional(<NSHTTPURLResponse: 0x62800002cb80> { URL: https://i1.wp.com/pixan.wpengine.com/wp-content/uploads/2016/07/canasta-pareja.jpg?fit=600%2C600&ssl=1 } { status code: 200, headers {
"Cache-Control" = "public, max-age=63115200";
"Content-Length" = 24757;
"Content-Type" = "image/jpeg";
Date = "Thu, 01 Dec 2016 06:06:41 GMT";
Etag = "\"629f656831de2958\"";
Expires = "Sat, 01 Dec 2018 18:00:39 GMT";
"Last-Modified" = "Thu, 01 Dec 2016 06:00:39 GMT";
Link = "<https://pixan.wpengine.com/wp-content/uploads/2016/07/canasta-pareja.jpg>; rel=\"canonical\"";
Server = nginx;
Vary = Accept;
"x-bytes-saved" = 2467;
"x-content-type-options" = nosniff;
"x-nc" = "HIT bur 66";
} })
SUCCESS
<UIImage: 0x6280002830c0>, {300, 300}
2016-12-01 00:06:41.110589 pixan[9961:2940658] [] 

但我仍然犯同样的错误。

最佳答案

var urlstr = ""   
if let urlString = productPictures[indexPath.item] as? String
{
 urlstr = urlString
}
cell.imageView.sd_setImageWithURL(NSURL(string: urlstr!), placeholderImage: UIImage(named: "imgPlaceHolder"), completed: { (image, error, cacheType, imageURL) -> Void in})

关于swift - 使用 AlamoFireImage 在 CollectionView 中使用 .af_setImage 方法加载数组的 url 时发生 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40903484/

相关文章:

swift - 'hideUISearchBarWhenScrolling'-属性不作为属性与 tableView 一起使用

swift - 开关盒支架

ios - 按照维护顺序快速解析 json

ios - 在 StoryBoard 中调整 Collection View 标题的大小

ios - UITableViewCell 中的 UICollectionVIew 如何使用自动布局获取动态高度

swift - 崩溃 : Can only call -[PFObject init] on subclasses conforming to PFSubclassing

ios - 具有大量数据 Swift 的 CollectionView

ios - UITableView 和 UICollectionView 之间同步滚动 : lag when using multiple scrollToRowAtIndexPath

ios - 尝试确定子类 UICollectionViewCell 中的单元格宽度

ios - Swift:如何在写入时不断调整 UICollectionViewCell 高度以适应 UITextView?