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

时间:2017-03-08 12:15:36

标签: ios swift sdwebimage

我无法在两个使用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语句运行..)

3 个答案:

答案 0 :(得分:1)

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
            }
        }

答案 1 :(得分:0)

我认为它可能就像这样

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)

用于第二个imageview

答案 2 :(得分:0)

尝试使用此功能:

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