SDWebimage占位符中的活动指示器

时间:2014-03-04 09:59:53

标签: iphone ios7 uiimageview xcode5

如何使用SDWebImage

UIImageView占位符中设置活动指示器

6 个答案:

答案 0 :(得分:1)

Swift 4

imgView.sd_setShowActivityIndicatorView(true)
imgView.sd_setIndicatorStyle(.gray)
imgView.sd_setImage(with: URL(string: imageURL))

经过测试和工作

答案 1 :(得分:1)

SdWebImage 5.0

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

答案 2 :(得分:1)

Swift 5和SDWebImage 5-beta

//pod file
pod 'SDWebImage', '~> 5.0.0-beta3'

//import 
import SDWebImage

并设置指示器和图像

myImageView.sd_imageIndicator = SDWebImageActivityIndicator.gray
myImageView.sd_setImage(with: URL(string: "http://url", completed: nil)

答案 3 :(得分: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)
          }
      }
    }
 }

用法:

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

答案 4 :(得分:0)

[indicator startAnimating]; //指标开始制作动画

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

    }];

答案 5 :(得分:0)

我猜它为时已晚,但对于仍在寻找的人来说,这是一个解决方案。 SDWebImage库默认包含它。

对我来说就像一个魅力:

斯威夫特3:

imgView.setShowActivityIndicator(true)
imgView.setIndicatorStyle(.gray)
imgView.sd_setImage(with: URL(string: urlString), placeholderImage: UIImage(named: "placeholder"))