André
André

Reputation: 143

Unable to set activity indicator using SDWebImage

I'm unable to set an activity indicator in two images that use the SDWebImage for downloading pictures asynchronously. Here's my code:

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

I know that the if-loop is running(the print statement inside it runs..)

Upvotes: 0

Views: 1571

Answers (3)

Andrian Rahardja
Andrian Rahardja

Reputation: 344

Swift 3 & SDWebImage 4.0.0

This will do the trick, try this code

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

Upvotes: 1

Dheeraj D
Dheeraj D

Reputation: 4451

Try using this:

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

Upvotes: 0

abdullahselek
abdullahselek

Reputation: 8435

I thinks it might be like this

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

You duplicated

backgroundImage.sd_setShowActivityIndicatorView(true)

instead of using

foregroundImage.sd_setShowActivityIndicatorView(true)

for the second imageview

Upvotes: 0

Related Questions