Mhmd Backer Shehadi
Mhmd Backer Shehadi

Reputation: 589

Activity indicator in SDWebimage placeholder

How can I set an Activity indicator in SDWebImage placeholder using UIImageView

Upvotes: 7

Views: 6241

Answers (8)

Evgeny Karkan
Evgeny Karkan

Reputation: 9612

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()

Upvotes: 1

Rohit Nishad
Rohit Nishad

Reputation: 448

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

Upvotes: -1

Raj
Raj

Reputation: 25

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

Upvotes: 1

Ahmadreza
Ahmadreza

Reputation: 7212

SdWebImage 5.0

YOUR_IMAGEVIEW.sd_imageIndicator = SDWebImageActivityIndicator.gray
YOUR_IMAGEVIEW.sd_imageIndicator?.startAnimating()

Upvotes: 3

Ankit Kumar Gupta
Ankit Kumar Gupta

Reputation: 4042

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

Upvotes: 3

saurabh
saurabh

Reputation: 6775

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

Upvotes: 4

Lahiru Pinto
Lahiru Pinto

Reputation: 1681

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)

Upvotes: 4

Rushabh
Rushabh

Reputation: 3203

[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

    }];

Upvotes: 0

Related Questions