简体   繁体   中英

How to import and use SDWebImage in swift xcode 6.3.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

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)

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"

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

    }
}

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM