UIWebView的离线缓存

本博客的原则是:不发则已,要发就发牛逼的。不指望上推荐,只希望发些精髓的东西,与业界的朋友共同成长。

相信不少朋友用过UIWebView,webView下载的图片一般比较大,这个要能缓存就好了,可以大幅度提高加载速度,同时为用户节省流量。本文就是讲如何完美解决webView缓存的问题。

实际上,UIWebView自己是有缓存的,但容量有限,清理时间我们也不好掌握,那它是用什么做的缓存呢?是NSURLCache。看到它有几个方法:

+ (void)setSharedURLCache:(NSURLCache *)cache;

- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request;

- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request;

太好了,我们只要写一个子类继承NSURLCache,实现后两个方法,再让这个子类对象成为sharedURLCache,就可以操控webView的请求和缓存了。抛个砖吧:

- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request {
                                                                                                                                                                                                                          
    NSString *pathString = [[request URL] absoluteString];
                                                                                                                                                                                                                          
    if (![pathString hasSuffix:@".jpg"]) {
        return [super cachedResponseForRequest:request];
    }
                                                                                                                                                                                                                          
    if ([[MYURLCache sharedCache] hasDataForURL:pathString]) {
        NSData *data = [[MYURLCache sharedCache] dataForURL:pathString];
        NSURLResponse *response = [[[NSURLResponse alloc] initWithURL:[request URL]
                                                             MIMEType:@"image/jpg"
                                                expectedContentLength:[data length]
                                                     textEncodingName:nil] autorelease];
        return [[[NSCachedURLResponse alloc] initWithResponse:response data:data] autorelease];        
    }
    return [super cachedResponseForRequest:request];
}
                                                                                                                                                                                                                      
- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request {
    NSString *pathString = [[request URL] absoluteString];
    if (![pathString hasSuffix:@".jpg"]) {
        [super storeCachedResponse:cachedResponse forRequest:request];
        return;
    }
                                                                                                                                                                                                                          
    [[MYURLCache sharedCache] storeData:cachedResponse.data forURL:pathString];
}

上面的代码是专门用来搞定webView中的jpg图片的,其中MYURLCache提供了把data读、写入文件的功能,这个不是本文的重点,请各位自己实现吧。

在程序启动的时候,加入以下代码:

MYURLCache *cache = [[MYURLCache alloc] init];
[NSURLCache setSharedURLCache:cache];

OK,搞定了,试试webView加载图片吧~

转载于:https://www.cnblogs.com/sany007/archive/2013/02/22/2922816.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值