3

I'm using SDWebImage and I would like to add a loading indicator. I found the UIActivityIndicator-for-SDWebImage library, and I have tried to add it to my Swift project by adding this:

#import "UIImageView+UIActivityIndicatorForSDWebImage.h"

to my bridging header. For some reason I still can't access the extra parameter usingActivityIndicatorStyle when setting image URLs.

Anyone have any idea what I'm doing wrong? I've tried removing the .m and .h files and re-adding them, but it didn't help.

user3746428
  • 11,047
  • 20
  • 81
  • 137

3 Answers3

4
  1. Add "use_frameworks!" in the Podfile

    use_frameworks!
    
    target '<YOUR TARGET>' do
    .........
        pod 'UIActivityIndicator-for-SDWebImage'
    .........
    end
    
  2. In "Pods -> UIActivityIndicator-for-SDWebImage -> UIImageView+UIActivityIndicatorForSDWebImage.h" replace:

    #import "UIImageView+WebCache.h"
    #import "SDImageCache.h"
    

    With:

    #import <SDWebImage/UIImageView+WebCache.h>
    #import <SDWebImage/SDImageCache.h> 
    
  3. In "AppDelegate.swift" add

    import UIActivityIndicator_for_SDWebImage
    

    That's all.

P.S. You should correct Pods because author of UIActivityIndicator-for-SDWebImage wrote in description: "... I really don't want to keep this repo updated ...", and he really doesn't do it.

Ted
  • 22,696
  • 11
  • 95
  • 109
Igor
  • 12,165
  • 4
  • 57
  • 73
  • Will #import also work on pure Objective-C project? If so, I think the pod should update to this one so it works on both Objective-C and Swift. – John Pang Jul 19 '18 at 15:19
  • Sure, this pod wrote on Objective C and will work on both Objective-C and Swift projects. – Igor Jul 20 '18 at 00:42
  • I know this pod works on both Obj-C and Swift project. But the #import "SDImageCache.h" doesn't - we must unlock the file and modify it to #import <...>. However, on a pod update or reinstall, it revert to the git version. So, if "..." works for Obj-C only but <...> works on both, it is better to use <...> over "..." – John Pang Jul 21 '18 at 02:38
  • UIImageView+UIActivityIndicatorForSDWebImage.h, where changes should be done is Objective C file. Sure, on pod update changes will reverted. As this pod is not supported more, one way to prevent this is to create own fork and use it. – Igor Jul 21 '18 at 11:46
3

Swift 3 & SDWebImage 4.0.0

This will do the trick, try this code

        let urlString = URL(string: "Your image 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
            }
        }
1

The answer above works

Follow this issue https://github.com/JJSaccolo/UIActivityIndicator-for-SDWebImage/pull/28

You will find a fork which has made this update

https://github.com/masterfego/UIActivityIndicator-for-SDWebImage

I forked it too,

https://github.com/useeless37/UIActivityIndicator-for-SDWebImage

then in your pod file

pod 'UIActivityIndicator-for-SDWebImage', :git => 'https://github.com/useeless37/UIActivityIndicator-for-SDWebImage.git'
Ted
  • 22,696
  • 11
  • 95
  • 109