1

Im new to Swift and now making a project that includes showing many photos from the web and I understand that I need to use SDWebImage. I saw related questions here and in other places but all are in Objective-C syntax and not working for me.

What I did till now:

  1. I downloaded the zip from GitHub
  2. Copied SDWebImage folder to my folder
  3. Tried all possible combinations for import

#import <"SDWebImage/UIImageView+WebCache.h">

import SDWebImage/UIImageView+WebCache.h

etc..

Can someone please help me import it

Azat
  • 6,745
  • 5
  • 31
  • 48
user2993422
  • 306
  • 2
  • 6
  • 22

5 Answers5

10

Firstly, you need configure Bridging Header, it is described here at SO. Use the following:

#import <SDWebImage/UIImageView+WebCache.h>

This will import Objective-C code to Swift code

Secondly, just use it, like:

self.imageView.sd_setImageWithURL(self.imageURL)
Community
  • 1
  • 1
Azat
  • 6,745
  • 5
  • 31
  • 48
1

As far as I understood you already have a Bridging header and only need to organize your imports. Since you copied the source files directly into your project without using cocoapods or Carthage you do the import like this:

#import "UIImageView+WebCache.h"
t0day
  • 121
  • 8
1

Swift 3.0 code

import SDWebImage

let url = URL.init(string: "https://vignette3.wikia.nocookie.net/zelda/images/b/b1/Link_%28SSB_3DS_%26_Wii_U%29.png")

  imagelogo.sd_setImage(with: url , placeholderImage: nil)
Raksha Saini
  • 604
  • 12
  • 28
Arjun Yadav
  • 1,369
  • 1
  • 10
  • 23
1

In swift 5.1 simply import the SdWebimage and use extenion

extension UIImageView{

func downloadImage(url : String, placeHolder : UIImage?) throws{
    if let url = URL(string: url){
        self.sd_imageIndicator?.startAnimatingIndicator()
        self.sd_setImage(with: url) { (image, err, type, url) in

            if err != nil{
                    self.sd_imageIndicator?.stopAnimatingIndicator()
                    print("Failed to download image")
                }
                self.sd_imageIndicator?.stopAnimatingIndicator()
            }
        }

    }
}
Talha Rasool
  • 1,126
  • 14
  • 12
0

The only way it worked for me (using mac os 10.11.4 and ios 9.3) was this:

  1. download and unpack framework from link: https://github.com/rs/SDWebImage/releases/download/3.6/SDWebImage-3.6.framework.zip:

  2. drag that framework into x-code project and check option: Copy items if needed (In project navigator you should now see suitcase icon containing a folder with .h files)

  3. create new bridging header file: cmd+n -> file -> new -> file... and select "header" template icon
  4. open that file, write line (just above #endif:) #import < SDWebImage/UIImageView+WebCache.h > (this is path of one of header files from framework)

  5. the most important step from @Azat answer above, is to connect the file you just made with project, in build settings:

    • select project name (blue icon in Project navigator)
    • select "Build Settings" on your right start to type "Bridging header..." and when you have row containing "Objective-C Bridging header"
    • press twice in empty field in that row (window pops-up), and drag bridging header file from Project navigator into that window.

      1. hopefully now you can use library methods as: imageView.setImageWithUrl(url, placeholderImage:)
mark_dimitry
  • 81
  • 1
  • 2