CocoaPods安装SDWebImage WebP

2,657 阅读2分钟

最近在项目开发过程中,需要使用到WebP格式的图片,所以要安装SDWebImage/WebP子库,修改下Podfile:

# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'

target 'OptimusPrime' do
  # Uncomment the next line if you are using Swift or would like to use dynamic frameworks
  # use_frameworks!

  # Pods for OptimusPrime 
  pod 'SDWebImage'
  pod 'SDWebImage/WebP'

end

然后 pod install 发现如下错误:

OptimusPrime git:(master) ✗ pod install
Analyzing dependencies
Downloading dependencies
Installing SDWebImage 5.3.1 (was 5.3.1)
Installing libwebp (1.0.3)

[!] Error installing libwebp
[!] /usr/bin/git clone https://chromium.googlesource.com/webm/libwebp /var/folders/8j/9jfm_2xx54q89tzs0sfn7nxr0000gn/T/d20180613-76555-1d5w7hk --template= --single-branch --depth 1 --branch v0.6.1

Cloning into '/var/folders/8j/9jfm_2xx54q89tzs0sfn7nxr0000gn/T/d20180613-76555-1d5w7hk'...
fatal: unable to access 'https://chromium.googlesource.com/webm/libwebp/': Failed to connect to chromium.googlesource.com port 443: Operation timed out

原因:chromium.googlesource.com 被墙

解决办法1:让Mac终端走代理

打开小飞机,自动代理模式

1.如果你使用的是zsh:

$ vim ~/.zshrc

添加如下代理配置(完了按:wq保存退出):

# proxy list
alias proxy='export all_proxy=socks5://127.0.0.1:1080'
alias unproxy='unset all_proxy'

然后

$ source ~/.zshrc

2.如果你使用的是Mac自带终端:

$ vim ~/.bashrc

添加如下代理配置(完了按:wq保存退出):

# proxy list
alias proxy='export all_proxy=socks5://127.0.0.1:1080'
alias unproxy='unset all_proxy'

然后

$ source ~/.bashrc

最后执行pod install即可

解决办法2:使用镜像

观察SDWebImage仓库的SDWebImage.podspec文件会发现以下配置:

s.subspec 'WebP' do |webp|
webp.source_files = 'SDWebImage/UIImage+WebP.{h,m}', 'SDWebImage/SDWebImageWebPCoder.{h,m}'
webp.xcconfig = { 
  'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) SD_WEBP=1',
  'USER_HEADER_SEARCH_PATHS' => '$(inherited) $(SRCROOT)/libwebp/src'
}
webp.watchos.xcconfig = {
  'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) SD_WEBP=1 WEBP_USE_INTRINSICS=1',
  'USER_HEADER_SEARCH_PATHS' => '$(inherited) $(SRCROOT)/libwebp/src'
}
webp.dependency 'SDWebImage/Core'
webp.dependency 'libwebp', '~> 0.5'
end

SDWebImage/WebP依赖libwebp库,但是安装libwebp库的时候无法指定源,所以要先安装libwebp(此时换成github镜像),再安装SDWebImage/WebP即可

查看mac中cocoapods本地库对应的libwebp版本的配置文件 终端输入find ~/.cocoapods/repos/master -iname libwebp(10秒后按Ctrl+C终止查找),会得到一个SDWebImage/WebP依赖的库的spec路径

> find ~/.cocoapods/repos/master -iname libwebp
/Users/you_mac_name/.cocoapods/repos/master/Specs/1/9/2/libwebp

Finder进入/Users/you_mac_name/.cocoapods/repos/master/Specs/1/9/2/libwebp此目录会发现几个版本的libwebp库spec文件:

这里打开1.0.3文件夹,复制libwebp.podspec.json文件到自己项目根目录 修改libwebp.podspec.json文件:

"git": "https://chromium.googlesource.com/webm/libwebp"
为
"git": "https://github.com/webmproject/libwebp"

然后运行 pod install 即可