我从我的REST API获取了一个url数组,我想使用它们从我的集合视图单元格的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
}
}
奇怪的是调试控制台会抛出这个:
这意味着价值观存在,但它一直让我失望 致命错误:
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] []
但我仍然犯了同样的错误。
答案 0 :(得分:0)
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})