Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios af_setImage-在展开可选值时意外发现nil_Ios_Swift_Xcode - Fatal编程技术网

Ios af_setImage-在展开可选值时意外发现nil

Ios af_setImage-在展开可选值时意外发现nil,ios,swift,xcode,Ios,Swift,Xcode,我想从服务器端加载图像,但af_setImage显示此错误: fatal error: unexpectedly found nil while unwrapping an Optional value 我的代码: override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let ce

我想从服务器端加载图像,但
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!
}

更新

我的图像视图返回空值:

if(cell.img_main==nil) { 打印(“测试”) }

试试这个:

    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!")
    }

这样错误就不会使你的应用程序崩溃。

我们观察到类似的问题,经过多次调试,我们发现服务器返回的内容类型为=“image/*”。AlamofireImage无法接受,它返回的响应为零

我们可以通过使用“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)

单元的@IBOutlet img_main是否连接到Interface Builder小部件?是。它已连接。假设
af_setImage
是一个
UIImageView,我猜它没有正确连接到nib。