UIWebView的离线缓存
3536 点击·0 回帖
![]() | ![]() | |
![]() | 本博客的原则是:不发则已,要发就发牛逼的。不指望上推荐,只希望发些精髓的东西,与业界的朋友共同成长。 相信不少朋友用过UIwebView,webView下载的图片一般比较大,这个要能缓存就好了,可以大幅度提高加载速度,同时为用户节省流量。本文就是讲如何完美解决webView缓存的问题。 实际上,UIWebView自己是有缓存的,但容量有限,清理时间我们也不好掌握,那它是用什么做的缓存呢?是NSURLCache。看到它有几个方法: + ([backcolor=transparent]void)setSharedURLCache:([backcolor=transparent]NSURLCache *)cache; - ([backcolor=transparent]NSCachedURLResponse *)cachedResponseForRequest:([backcolor=transparent]NSURLRequest *)request; - ([backcolor=transparent]void)storeCachedResponse:([backcolor=transparent]NSCachedURLResponse *)cachedResponse forRequest:([backcolor=transparent]NSURLRequest *)request; 太好了,我们只要写一个子类继承NSURLCache,实现后两个方法,再让这个子类对象成为sharedURLCache,就可以操控webView的请求和缓存了。抛个砖吧: [backcolor=white !important] [align=right !important][backcolor=white !important]1 [align=right !important][backcolor=white !important]2 [align=right !important][backcolor=white !important]3 [align=right !important][backcolor=white !important]4 [align=right !important][backcolor=white !important]5 [align=right !important][backcolor=white !important]6 [align=right !important][backcolor=white !important]7 [align=right !important][backcolor=white !important]8 [align=right !important][backcolor=white !important]9 [align=right !important][backcolor=white !important]10 [align=right !important][backcolor=white !important]11 [align=right !important][backcolor=white !important]12 [align=right !important][backcolor=white !important]13 [align=right !important][backcolor=white !important]14 [align=right !important][backcolor=white !important]15 [align=right !important][backcolor=white !important]16 [align=right !important][backcolor=white !important]17 [align=right !important][backcolor=white !important]18 [align=right !important][backcolor=white !important]19 [align=right !important][backcolor=white !important]20 [align=right !important][backcolor=white !important]21 [align=right !important][backcolor=white !important]22 [align=right !important][backcolor=white !important]23 [align=right !important][backcolor=white !important]24 [align=right !important][backcolor=white !important]25 [align=right !important][backcolor=white !important]26 [align=right !important][backcolor=white !important]27 [align=right !important][backcolor=white !important]28 [backcolor=initial !important][backcolor=white !important]- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request { [backcolor=white !important] [backcolor=white !important] NSString *pathString = [[request URL] absoluteString]; [backcolor=white !important] [backcolor=white !important] if (![pathString hasSuffix:@".jpg"]) { [backcolor=white !important] return [super cachedResponseForRequest:request]; [backcolor=white !important] } [backcolor=white !important] [backcolor=white !important] if ([[MYURLCache sharedCache] hasDataForURL:pathString]) { [backcolor=white !important] NSData *data = [[MYURLCache sharedCache] dataForURL:pathString]; [backcolor=white !important] NSURLResponse *response = [[[NSURLResponse alloc] initWithURL:[request URL] [backcolor=white !important] MIMEType:@"image/jpg" [backcolor=white !important] expectedContentLength:[data length] [backcolor=white !important] textEncodingName:nil] autorelease]; [backcolor=white !important] return [[[NSCachedURLResponse alloc] initWithResponse:response data:data] autorelease]; [backcolor=white !important] } [backcolor=white !important] return [super cachedResponseForRequest:request]; [backcolor=white !important]} [backcolor=white !important] [backcolor=white !important]- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request { [backcolor=white !important] NSString *pathString = [[request URL] absoluteString]; [backcolor=white !important] if (![pathString hasSuffix:@".jpg"]) { [backcolor=white !important] [super storeCachedResponse:cachedResponse forRequest:request]; [backcolor=white !important] return; [backcolor=white !important] } [backcolor=white !important] [backcolor=white !important] [[MYURLCache sharedCache] storeData:cachedResponse.data forURL:pathString]; [backcolor=white !important]} 上面的代码[backcolor=transparent]是专门用来搞定webView中的jpg图片的,其中MYURLCache提供了把data读、写入文件的功能,这个不是本文的重点,请各位自己实现吧。 在程序启动的时候,加入以下代码: [backcolor=white !important] [align=right !important][backcolor=white !important]1 [align=right !important][backcolor=white !important]2 [backcolor=initial !important][backcolor=white !important]MYURLCache *cache = [[MYURLCache alloc] init]; [backcolor=white !important][NSURLCache setSharedURLCache:cache]; OK,搞定了,试试webView加载图片吧~ | |
![]() | ![]() |