2017-03-08 57 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 
      } 
     }