Ios 无法使用SDWebImage设置活动指示器

Ios 无法使用SDWebImage设置活动指示器,ios,swift,sdwebimage,Ios,Swift,Sdwebimage,我无法在使用SDWebImage异步下载图片的两个图像中设置活动指示器。这是我的密码: if let imgId = experimentFetched[0].backgroundImageId { print("Inside if") backgroundImage.sd_setIndicatorStyle(.gray) backgroundImage.sd_setShowActivityIndicatorView(tr

我无法在使用SDWebImage异步下载图片的两个图像中设置活动指示器。这是我的密码:

 if let imgId = experimentFetched[0].backgroundImageId {
            print("Inside if")
            backgroundImage.sd_setIndicatorStyle(.gray)
            backgroundImage.sd_setShowActivityIndicatorView(true)
            backgroundImage.sd_setImage(with: URL(string:    Constants.apiBaseUrl + "Images/" + imgId), placeholderImage: UIImage(named: "stock"))
            foregroundImage.sd_setIndicatorStyle(.gray)
            backgroundImage.sd_setShowActivityIndicatorView(true)
            foregroundImage.sd_setImage(with: URL(string: Constants.apiBaseUrl + "Images/" + imgId), placeholderImage: UIImage(named: "stock"))
        }

我知道if循环正在运行(其中的print语句正在运行)

我认为可能是这样的

if let imgId = experimentFetched[0].backgroundImageId {
        print("Inside if")
        backgroundImage.sd_setIndicatorStyle(.gray)
        backgroundImage.sd_setShowActivityIndicatorView(true)
        backgroundImage.sd_setImage(with: URL(string:    Constants.apiBaseUrl + "Images/" + imgId), placeholderImage: UIImage(named: "stock"))
        foregroundImage.sd_setIndicatorStyle(.gray)
        foregroundImage.sd_setShowActivityIndicatorView(true)
        foregroundImage.sd_setImage(with: URL(string: Constants.apiBaseUrl + "Images/" + imgId), placeholderImage: UIImage(named: "stock"))
    }
你复制了

backgroundImage.sd_setShowActivityIndicatorView(true)
而不是使用

foregroundImage.sd_setShowActivityIndicatorView(true)

对于第二个图像视图,请尝试使用以下命令:

    if let imgId = experimentFetched[0].backgroundImageId {

    DispatchQueue.main.async {
        print("Inside if")
            backgroundImage.sd_setIndicatorStyle(.gray)
            backgroundImage.sd_setShowActivityIndicatorView(true)
            backgroundImage.sd_setImage(with: URL(string:    Constants.apiBaseUrl + "Images/" + imgId), placeholderImage: UIImage(named: "stock"))
            foregroundImage.sd_setIndicatorStyle(.gray)
            foregroundImage.sd_setShowActivityIndicatorView(true)
            foregroundImage.sd_setImage(with: URL(string: Constants.apiBaseUrl + "Images/" + imgId), placeholderImage: UIImage(named: "stock"))
    }
}

Swift 3和SDWebImage 4.0.0

这样就可以了,试试下面的代码

        // if you don't use placeholder use this
        let urlString = URL(string: url)
        yourImageView.sd_setIndicatorStyle(.gray)
        yourImageView.sd_setShowActivityIndicatorView(true)
        yourImageView.sd_setImage(with: urlString) { (loadedImage, error, cacheType, url) in
            yourImageView.sd_removeActivityIndicator()
            if error != nil {
                print("Error code: \(error!.localizedDescription)")
            } else {
                yourImageView.image = loadedImage
            }
        }

        // if you use image placeholder use this instead
        let urlString = URL(string: url)
        yourImageView.sd_setIndicatorStyle(.gray)
        yourImageView.sd_setShowActivityIndicatorView(true)
        yourImageView.sd_setImage(with: urlString, placeholderImage: UIImage("Your placeholeder image name")) { (loadedImage, error, cacheType, url) in
            yourImageView.sd_removeActivityIndicator()
            if error != nil {
                print("Error code: \(error!.localizedDescription)")
            } else {
                yourImageView.image = loadedImage
            }
        }

您可以通过简单的方式使用此插件: