zoukankan      html  css  js  c++  java
  • UIWebView的离线缓存

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

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

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

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

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    - (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读、写入文件的功能,这个不是本文的重点,请各位自己实现吧。

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

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

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

  • 相关阅读:
    元旦发布DayPilot Pro 5.8源代码
    新年新开端
    PWA 2007 过期解决办法
    Document Library Explorer 2007 源代码更新下载
    发布DayPilotPro5.5.1780 SP1源代码
    MOSS自带调查列表结果图形化展示
    自定义PWA报工界面:隐藏加班项
    学习Document Library Explorer 2007源代码
    WSS&MOSS SP3 Now Available
    SQL Server 2008安装失败
  • 原文地址:https://www.cnblogs.com/-ios/p/4763208.html
Copyright © 2011-2022 走看看