如何在 swift xcode 6.3.1 中导入和使用 SDWebImage
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30004681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to import and use SDWebImage in swift xcode 6.3.1
提问by user2993422
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.
我是新手Swift
,现在正在制作一个项目,其中包括展示来自网络的许多照片,我知道我需要使用SDWebImage
. 我在这里和其他地方看到了相关的问题,但都是Objective-C
语法问题,对我不起作用。
What I did till now:
到目前为止我所做的:
- I downloaded the zip from GitHub
- Copied
SDWebImage
folder to my folder - Tried all possible combinations for
import
- 我从 GitHub 下载了 zip
- 复制
SDWebImage
文件夹到我的文件夹 - 尝试了所有可能的组合
import
#import <"SDWebImage/UIImageView+WebCache.h">
#import <"SDWebImage/UIImageView+WebCache.h">
import SDWebImage/UIImageView+WebCache.h
import SDWebImage/UIImageView+WebCache.h
etc..
等等..
Can someone please help me import it
有人可以帮我导入吗
回答by Azat
Firstly, you need configure Bridging Header
, it is described here at SO. Use the following:
首先,您需要 configure Bridging Header
,在 SO 中对此进行了描述。使用以下内容:
#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)
回答by t0day
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"
回答by Talha Rasool
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()
}
}
}
}
回答by mark_dimitry
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:
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)
- create new bridging header file: cmd+n -> file -> new -> file... and select "header" template icon
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:
- 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.
- hopefully now you can use library methods as: imageView.setImageWithUrl(url, placeholderImage:)
从链接下载和解压框架:https://github.com/rs/SDWebImage/releases/download/3.6/SDWebImage-3.6.framework.zip:
将该框架拖到 x-code 项目中并选中选项:如果需要,请复制项目(在项目导航器中,您现在应该看到包含带有 .h 文件的文件夹的手提箱图标)
- 创建新的桥接头文件:cmd+n -> file -> new -> file... 并选择“header”模板图标
打开该文件,写一行(就在#endif 之上:) #import < SDWebImage/UIImageView+WebCache.h > (这是框架中的头文件之一的路径)
上面@Azat 回答中最重要的一步是在构建设置中将您刚刚制作的文件与项目连接起来:
- 选择项目名称(项目导航器中的蓝色图标)
- 选择“构建设置”在你的右边开始输入“Bridging header...”,当你有包含“Objective-C Bridging header”的行时
在该行的空白字段中按两次(弹出窗口),然后将桥接头文件从项目导航器拖到该窗口中。
- 希望现在你可以使用库方法: imageView.setImageWithUrl(url, placeholderImage:)
回答by Arjun Yadav
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)