[英]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
.我是
Swift
新手,现在正在制作一个项目,其中包括展示来自网络的许多照片,我知道我需要使用SDWebImage
。 I saw related questions here and in other places but all are in Objective-C
syntax and not working for me.我在这里和其他地方看到了相关问题,但都是
Objective-C
语法,对我不起作用。
What I did till now:到目前为止我所做的:
SDWebImage
folder to my folderSDWebImage
文件夹复制到我的文件夹import
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 .首先,您需要配置
Bridging Header
, 在 SO 中对此进行了描述。 Use the following:使用以下内容:
#import <SDWebImage/UIImageView+WebCache.h>
This will import Objective-C
code to Swift
code这会将
Objective-C
代码导入Swift
代码
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:
由于您不使用 cocoapods 或 Carthage 将源文件直接复制到您的项目中,因此您可以像这样进行导入:
#import "UIImageView+WebCache.h"
In swift 5.1 simply import the SdWebimage and use extenion在 swift 5.1 中,只需导入 SdWebimage 并使用扩展名
extension UIImageView{扩展 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:它对我有用的唯一方法(使用 mac os 10.11.4 和 ios 9.3)是这样的:
download and unpack framework from link: https://github.com/rs/SDWebImage/releases/download/3.6/SDWebImage-3.6.framework.zip :从链接下载和解压框架: 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)将该框架拖到 x-code 项目中并选中选项:如果需要,请复制项目(在项目导航器中,您现在应该看到包含带有 .h 文件的文件夹的手提箱图标)
open that file, write line (just above #endif:) #import < SDWebImage/UIImageView+WebCache.h > (this is path of one of header files from framework)打开该文件,写一行(就在#endif 之上:)#import < SDWebImage/UIImageView+WebCache.h > (这是框架中的头文件之一的路径)
the most important step from @Azat answer above, is to connect the file you just made with project, in build settings:上面@Azat 回答中最重要的一步是在构建设置中将您刚刚制作的文件与项目连接起来:
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斯威夫特 3.0 代码
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.