WebP是什么?
WebP(發(fā)音 weppy防症,來自:Google WebP)溅漾,是一種支持有損壓縮和無損壓縮的圖片文件格式懈费,派生自圖像編碼格式 VP8计露。根據(jù) Google 的測(cè)試,無損壓縮后的 WebP 比 PNG 文件少了 45% 的文件大小憎乙,即使這些 PNG 文件經(jīng)過其他壓縮工具壓縮之后票罐,WebP 還是可以減少 28% 的文件大小。在 Google 的明星產(chǎn)品如 Youtube泞边、Gmail该押、Google Play 中都可以看到 WebP 的身影,而 Chrome 網(wǎng)上商店甚至已完全使用了 WebP阵谚。國(guó)外公司如 Facebook蚕礼、ebay 和國(guó)內(nèi)公司如騰訊烟具、淘寶、美團(tuán)等也早已嘗鮮奠蹬。下面是QQ圖片格式對(duì)比圖
Snip20170803_18.png
iOS中如何使用WebP格式圖片?
很幸運(yùn),SDWebImage支持WebP格式圖片,可以講WebP數(shù)據(jù)–>NSData–>UIImage
There are 3 subspecs available now: Core, MapKit and WebP (this means you can install only some of the SDWebImage modules. By default, you get just Core, so if you need WebP, you need to specify it).Podfile example:$pod ‘SDWebImage/WebP’
摘自SDWebImage
PS:下載WebP這個(gè)庫(kù),由于是從Google下載的,如果下載失敗,請(qǐng)翻墻重試!!!
如果你是手動(dòng)添加WebP這個(gè)庫(kù)净赴,Xcode 需要如下配置 targets->build settings ->preprocessor Macros 填寫 SD_WEBP=1 如圖
到此app 中支持 WebP圖片基本完成,但是重點(diǎn)來了,由于部分app使用到了UIWebview/WKWebview 這兩個(gè)控件是不支持WebP圖片的,目前有兩種方式可以讓其支持WebP格式圖片。
實(shí)現(xiàn)方式一 NSURLProtocol
對(duì)于NSURLProtocol的作用及使用以后找個(gè)時(shí)間再講了~~~~可以參考Apple 開發(fā)者文檔NSURLProtocol, UIWebView 直接就可以支持,但是WKWebView是不支持的,如何讓W(xué)KWebView也支持NSURLProtocol?不過WKWebView自定義NSURLProtocol會(huì)丟失boay數(shù)據(jù)罩润。文章結(jié)尾會(huì)附上Demo下載地址。
WKWebView 拓展支持NSURLProtocol 具體代碼如下
FOUNDATION_STATIC_INLINE Class
ContextControllerClass() {
static Class cls;
if (!cls) {
cls = [[[WKWebView new] valueForKey:@"browsingContextController"] class];
}
return cls;
}
FOUNDATION_STATIC_INLINE SEL RegisterSchemeSelector() {
return NSSelectorFromString(@"registerSchemeForCustomProtocol:");
}
FOUNDATION_STATIC_INLINE SEL UnregisterSchemeSelector() {
return NSSelectorFromString(@"unregisterSchemeForCustomProtocol:");
}
@implementation NSURLProtocol (WebKitExt)
+ (void)wk_registerScheme:(NSString *)scheme {
Class cls = ContextControllerClass();
SEL sel = RegisterSchemeSelector();
if ([(id)cls respondsToSelector:sel]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[(id)cls performSelector:sel withObject:scheme];
#pragma clang diagnostic pop
}
}
+ (void)wk_unregisterScheme:(NSString *)scheme {
Class cls = ContextControllerClass();
SEL sel = UnregisterSchemeSelector();
if ([(id)cls respondsToSelector:sel]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[(id)cls performSelector:sel withObject:scheme];
#pragma clang diagnostic pop
}
}
好了,現(xiàn)在UIWebView與WKWebView 都已經(jīng)支持自定義NSURLProtocol了;
我們創(chuàng)建一個(gè)類CLURLProtocol 繼承自NSURLProtocol
下面這幾個(gè)方法必須實(shí)現(xiàn)
+(BOOL)canInitWithRequest:(NSURLRequest )request;
+(NSURLRequest )canonicalRequestForRequest:(NSURLRequest *)request;
-(void)stopLoading;
-(void)startLoading;
到此為止UIWebView/WKWebView 均已支持加載webp格式圖片
優(yōu)點(diǎn):
適合所有網(wǎng)頁,可以不用修改網(wǎng)頁內(nèi)部html內(nèi)容翼馆。
缺點(diǎn):
NSURLProtocol 攔截App 的所有請(qǐng)求, 使用時(shí)需要根據(jù)個(gè)人項(xiàng)目情況而定, WKWebView 在post請(qǐng)求時(shí)會(huì)丟失boay, 目前解決方式為在WKWebView的 開始加載的 代理方法判斷是否為post,為post解除注冊(cè)自定義的NSURLProtocol,為GET請(qǐng)求時(shí)注冊(cè)自定義NSURLProtocol割以。
最后
本文Demo:【BAWKWebView-WebP】
如有更多需求,請(qǐng)前往:【https://github.com/BAHome】