How can I set an Activity indicator in SDWebImage
placeholder using UIImageView
Swift 4
imgView.sd_setShowActivityIndicatorView(true)
imgView.sd_setIndicatorStyle(.gray)
imgView.sd_setImage(with: URL(string: imageURL))
Tested and working
Swift 5
imgView.sd_imageIndicator = SDWebImageActivityIndicator.gray
imgView.sd_setImage(with: URL(string: imageURL))
Swift 5 & SDWebImage 5-beta
//pod file
pod 'SDWebImage', '~> 5.0.0-beta3'
//import
import SDWebImage
and set indicator and image
myImageView.sd_imageIndicator = SDWebImageActivityIndicator.gray
myImageView.sd_setImage(with: URL(string: "http://url", completed: nil)
I guess its too late but here is a solution for anyone still looking. The SDWebImage library includes it by default.
Works like a charm for me :
Swift 3:
imgView.setShowActivityIndicator(true)
imgView.setIndicatorStyle(.gray)
imgView.sd_setImage(with: URL(string: urlString), placeholderImage: UIImage(named: "placeholder"))
Updates for Swift 5 : SDWebImage 5.x.x
imgView.sd_imageIndicator = SDWebImageActivityIndicator.gray
imgView.sd_setImage(with: URL(string: urlString), placeholderImage: UIImage(named: "placeholder"))
SdWebImage 5.0
YOUR_IMAGEVIEW.sd_imageIndicator = SDWebImageActivityIndicator.gray
YOUR_IMAGEVIEW.sd_imageIndicator?.startAnimating()
extension UIViewController{
func setUpBackGroundImage (imageUrl : String,finish: @escaping ((image:UIImage, error:Error?)) -> Void){
let networkIndicator = UIActivityIndicatorView()
var result:(image:UIImage, error:Error?) = (image:UIImage(imageLiteralResourceName: "game_bg"),error :nil)
networkIndicator.center = view.center
networkIndicator.style = .whiteLarge
networkIndicator.startAnimating()
view.addSubview(networkIndicator)
if imageUrl != ""{
SDWebImageManager.shared.loadImage(
with: URL(string: imageUrl),
options: .highPriority,
progress: nil) { (image, data, error, cacheType, isFinished, imageUrl) in
print(isFinished)
networkIndicator.stopAnimating()
networkIndicator.removeFromSuperview()
if (error != nil) {
result.error = error!
}else{
result.image = image!
}
finish(result)
}
}
}
}
Usage :
setUpBackGroundImage(imageUrl: backgroundImageURL, finish: { (arg0) in
let (image, error) = arg0
DispatchQueue.main.async {
if (error != nil) {
self.backgroundImageView.image = UIImage(named: "game_bg")
} else {
self.backgroundImageView.image = image
}
}
})
Swift 5, SDWebImage 5.15.8
To show activity indicator:
imageView.sd_imageIndicator = SDWebImageActivityIndicator.grayLarge
imageView.sd_imageIndicator?.startAnimatingIndicator()
Once image is downloaded, just stop it:
imageView.sd_imageIndicator?.stopAnimatingIndicator()
[indicator startAnimating];
//indicator start animating
//Load image
[imageView setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"placeholder.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
{
[indicator stopAnimating]; //indicator stop animating after image load
}];
Here is simple way you can add the activity indicator. you can also chnage color of indicator check below code
import SDWebImage
imageView.sd_imageIndicator = SDWebImageActivityIndicator.gray imgProfileOnTop.sd_setImage(with: URL(string: obj?.profileImg ?? ""), placeholderImage: UIImage(name:"placeholderImage"))
© 2022 - 2024 — McMap. All rights reserved.