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:
SDWebImage
folder to my folderimport
#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:
download and unpack framework from link: https://github.com/rs/SDWebImage/releases/download/3.6/SDWebImage-3.6.framework.zip :
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)
open that file, write line (just above #endif:) #import < SDWebImage/UIImageView+WebCache.h > (this is path of one of header files from framework)
the most important step from @Azat answer above, is to connect the file you just made with project, in build settings:
press twice in empty field in that row (window pops-up), and drag bridging header file from Project navigator into that window.
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.