Kingfisher默认是不支持webp格式图片,如果想要显示webp格式图片,得引入另一个库KingfisherWebP,具体操作如下
1 引入 KingfisherWebP 库
1) 在项目的Podfile文件中加入
$ pod 'KingfisherWebP'
2) 执行
$ pod install
正常情况下,就会报错了,别问我为什么知道一定会报错
[!] Error installing libwebp
[!] /usr/local/bin/git clone https://chromium.googlesource.com/webm/libwebp /var/folders/2f/yv5rlly926s0k1jdcs3m89lh0000gn/T/d20200110-6284-1uh2fk7 --template= --single-branch --depth 1 --branch v1.1.0
Cloning into '/var/folders/2f/yv5rlly926s0k1jdcs3m89lh0000gn/T/d20200110-6284-1uh2fk7'...
fatal: unable to access 'https://chromium.googlesource.com/webm/libwebp/': Failed to connect to chromium.googlesource.com port 443: Operation timed out
报错原因就是无法从指定的仓库中拉取到代码,那就给它换个仓库地址,修改pod repo中libwebp的git source 地址就行, 操作如下
1) 查看mac中cocoapods 本地库路径
$ pod repo
输出
- Type: git (master)
- URL: https://github.com/CocoaPods/Specs.git
- Path: /Users/darren/.cocoapods/repos/cocoapods
2) 在本地库中, 找到对应的libwebp版本的文件
$ find~/.cocoapods/repos/cocoapods-iname libwebp
输出
/Users/darren/.cocoapods/repos/cocoapods/Specs/1/9/2/libwebp
3) 进入libwebp目录,查看仓库中有哪些对应的版本
$ cd~/.cocoapods/repos/cocoapods/Specs/1/9/2/libwebp
$ ls-l
输出
total 0
drwxr-xr-x 3 darren staff 96 Jan 10 13:03 0.4.1
drwxr-xr-x 3 darren staff 96 Jan 10 13:03 0.4.2
drwxr-xr-x 3 darren staff 96 Jan 10 13:03 0.4.3
drwxr-xr-x 3 darren staff 96 Jan 10 13:03 0.4.4
drwxr-xr-x 3 darren staff 96 Jan 10 13:03 0.5.0
drwxr-xr-x 3 darren staff 96 Jan 10 13:03 0.5.1
drwxr-xr-x 3 darren staff 96 Jan 10 13:03 0.5.2
drwxr-xr-x 3 darren staff 96 Jan 10 13:03 0.6.0
drwxr-xr-x 3 darren staff 96 Jan 10 13:03 0.6.1
drwxr-xr-x 3 darren staff 96 Jan 10 13:03 1.0.0
drwxr-xr-x 3 darren staff 96 Jan 10 13:03 1.0.1
drwxr-xr-x 3 darren staff 96 Jan 10 13:03 1.0.2
drwxr-xr-x 3 darren staff 96 Jan 10 13:03 1.0.3
drwxr-xr-x 3 darren staff 96 Jan 10 13:03 1.1.0
drwxr-xr-x 3 darren staff 96 Jan 10 13:03 1.1.0-rc2
由于KingfisherWebP依赖的 libwebp版本为1.1.0(具体看刚才报错信息里面提示的版本),所以我们进入1.1.0中,并做修改
$ cd 1.1.0/
$ ls-l
输出
total 8
-rw-r--r-- 1 darren staff 1854 Jan 10 13:03 libwebp.podspec.json
4) 修改目录下的libwebp.podspec.json文件中git source
$ sudo vim libwebp.podspec.json
找到
"source": {
"git": "https://chromium.googlesource.com/webm/libwebp",
"tag": "v1.1.0"
},
将其中的"git" 对应的url替换为https://github.com/webmproject/libwebp.git,并保存
注: https://github.com/webmproject/libwebp.git 这是放在GitHub上的库,点击链接过去可以看到下面标注有 mirrored from https://chromium.googlesource.com/webm/libwebp,跟修改前的git source地址是一致的,所以放心大胆地用吧
5) 最后再执行pod install, 完成
Installing KingfisherWebP (0.7.0)
Installing libwebp (1.1.0)
Generating Pods project
Integrating client project
Pod installation complete! There are 37 dependencies from the Podfile and 45 total pods installed.
2 安装好KingfisherWebP后就可以在项目中用了,只需一步就行
import KingfisherWebP
self.kf.setImage(with:URL(string:string),placeholder:placeholder,options:[.processor(WebPProcessor.default),.cacheSerializer(WebPSerializer.default)]){(img,err,cacheType,imgUrl)in}
可以自定义一个延展,用起来更方便
extension KingfisherWrapper where Base: KFCrossPlatformImageView {
@discardableResult
public func setImage(urlString:String?, placeholder:Placeholder? = UIImage(named:"zhanwei")) -> DownloadTask? {
return setImage(with: URL(string: urlString ?? ""),
placeholder: placeholder,
options:[.transition(.fade(0.5)), .processor(WebPProcessor.default), .cacheSerializer(WebPSerializer.default)])
}
}
3 可以用以下链接的图片测试
webp图片