Activity indicator in SDWebimage placeholder
Asked Answered
N

8

6

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

Navarre answered 4/3, 2014 at 9:59 Comment(0)
F
4

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))
Forecourt answered 28/11, 2017 at 16:57 Comment(0)
L
4

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)
Listen answered 17/5, 2019 at 8:46 Comment(0)
H
3

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"))
Hitormiss answered 24/2, 2017 at 5:57 Comment(2)
I m using SDWebImage 5.0.2. Unfortunately it doesnt have these methodsAnkylostomiasis
Please find the updates. Kindly upvote if it wokrs for you. ThanksHitormiss
O
3

SdWebImage 5.0

YOUR_IMAGEVIEW.sd_imageIndicator = SDWebImageActivityIndicator.gray
YOUR_IMAGEVIEW.sd_imageIndicator?.startAnimating()
Octahedron answered 9/5, 2019 at 13:40 Comment(0)
C
1
    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
            }
        }
    })
Courlan answered 22/4, 2020 at 14:54 Comment(0)
T
1

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()
Thalassography answered 26/5, 2023 at 1:50 Comment(0)
C
0

[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

    }];
Carpel answered 4/3, 2014 at 10:21 Comment(3)
Where I should place the indicator ?Navarre
use the indicator on uiimageview.Carpel
[_newsimage addSubview:activityIndicator]; [_newsimage setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"placeholder.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) { [activityIndicator stopAnimating]; }]; // I am trying to do like this !Navarre
D
-1

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

Dinothere answered 14/10, 2021 at 12:39 Comment(1)
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.Oudh

© 2022 - 2024 — McMap. All rights reserved.