【问题标题】:How to import and use SDWebImage in swift xcode 6.3.1如何在 swift xcode 6.3.1 中导入和使用 SDWebImage
【发布时间】:2015-05-02 16:26:31
【问题描述】:
我是Swift 的新手,现在正在制作一个项目,其中包括展示来自网络的许多照片,我知道我需要使用SDWebImage。
我在这里和其他地方看到了相关的问题,但都在 Objective-C 语法中,不适合我。
到目前为止我做了什么:
- 我从 GitHub 下载了 zip
- 已将
SDWebImage 文件夹复制到我的文件夹中
- 尝试了
import 的所有可能组合
#import <"SDWebImage/UIImageView+WebCache.h">
import SDWebImage/UIImageView+WebCache.h
等等。
有人可以帮我导入吗
【问题讨论】:
标签:
ios
xcode
uitableview
swift
sdwebimage
【解决方案1】:
首先,您需要配置Bridging Header,它被描述为here at SO。使用以下内容:
#import <SDWebImage/UIImageView+WebCache.h>
这会将Objective-C 代码导入Swift 代码
其次,随便用,比如:
self.imageView.sd_setImageWithURL(self.imageURL)
【解决方案2】:
据我了解,您已经有一个桥接头,只需要组织您的导入。由于您在不使用 cocoapods 或 Carthage 的情况下将源文件直接复制到项目中,因此您可以像这样进行导入:
#import "UIImageView+WebCache.h"
【解决方案3】:
Swift 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)
【解决方案4】:
在 swift 5.1 中,只需导入 SdWebimage 并使用扩展
扩展 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()
}
}
}
}