我想从服务器端加载我的图片,但af_setImage
向我显示此错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
我的代码:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! SupportersCollectionViewCell
let url_img = URL(string: self.titles[indexPath.row])!
print(url_img)//it returns correct value like http://kashanmap.ir/images/apk_images/hamian/hamian2.png
cell.img_main.af_setImage(
withURL: url_img
)
return cell
}
我的自定义单元格:
import UIKit
import Alamofire
import AlamofireImage
class SupportersCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var img_main: UIImageView!
}
更新
我的图片视图返回null:
if(cell.img_main == nil) { 打印(“测试”) }
答案 0 :(得分:0)
只需尝试:
cell.img_main.image = nil
if let url_img = URL(string: self.titles[indexPath.row])
{
cell.img_main.af_setImage(withURL: url_img)
}
else
{
print("error!")
}
这样,错误不会使您的应用程序崩溃。
答案 1 :(得分:0)
我们观察到类似的问题,经过大量调试后,我们发现服务器返回的内容类型为“ image / *”。 AlamofireImage不可接受,它返回nil响应。
我们可以使用' ImageResponseSerializer '解决这个问题:
var imgSer = ImageResponseSerializer.self
imgSer.addAcceptableImageContentTypes(Set(arrayLiteral: "image/*"))
mView!.af.setImage( withURL: url,
placeholderImage: mPlaceholderImage,
**serializer: imgSer.init()**,
filter: self,
progress: nil, imageTransition: imgTransition!, runImageTransitionIfCached: true, completion: { dr in .....
AlamofireImage(3.6.0),Alamofire(4.9.1)