2017-03-08 59 views
0

我无法在两个使用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")) 
     } 

我知道,如果环是运行(print语句里面运行..)

+0

你可以使用这个插件一个简单的方法:https://github.com/JJSaccolo/UIActivityIndi​​cator-for-SDWebImage – Kingalione

回答

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的

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

斯威夫特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 
      } 
     }