【问题标题】:how to cache a whole web page with images in iOS如何在 iOS 中缓存带有图像的整个网页
【发布时间】:2011-10-05 11:39:38
【问题描述】:

我正在开发 iOS 上的图像-文本混合页面。我知道我可以使用 UIWebView 来实现目标。但问题是,用户可能需要离线阅读页面。 对于文本部分,我可以将 html 保存到磁盘并以离线模式加载。 但是图像呢?是否可以将图像缓存到磁盘,UIWebView 仍然可以显示它们?

谢谢!

【问题讨论】:

    标签: iphone ios ipad caching uiwebview


    【解决方案1】:

    ASIHTTPRequest project 有一个名为ASIWebPageRequest 的类,旨在完全按照您的意愿行事。如果您可以在项目中添加额外的依赖项,那么我认为这对您来说是一个很好的解决方案:ASIWebPageRequest

    在上面我喜欢的页面上,有一些很好的示例说明如何使用它,但为了完整起见,我将在此处包含其中一个示例:

    - (IBAction)loadURL:(NSURL *)url
    {
       // Assume request is a property of our controller
       // First, we'll cancel any in-progress page load
       [[self request] setDelegate:nil];
       [[self request] cancel];
    
       [self setRequest:[ASIWebPageRequest requestWithURL:url]];
       [[self request] setDelegate:self];
       [[self request] setDidFailSelector:@selector(webPageFetchFailed:)];
       [[self request] setDidFinishSelector:@selector(webPageFetchSucceeded:)];
    
       // Tell the request to embed external resources directly in the page
       [[self request] setUrlReplacementMode:ASIReplaceExternalResourcesWithData];
    
       // It is strongly recommended you use a download cache with ASIWebPageRequest
       // When using a cache, external resources are automatically stored in the cache
       // and can be pulled from the cache on subsequent page loads
       [[self request] setDownloadCache:[ASIDownloadCache sharedCache]];
    
       // Ask the download cache for a place to store the cached data
       // This is the most efficient way for an ASIWebPageRequest to store a web page
       [[self request] setDownloadDestinationPath:
          [[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:[self request]]];
    
       [[self request] startAsynchronous];
    }
    
    - (void)webPageFetchFailed:(ASIHTTPRequest *)theRequest
    {
       // Obviously you should handle the error properly...
       NSLog(@"%@",[theRequest error]);
    }
    
    - (void)webPageFetchSucceeded:(ASIHTTPRequest *)theRequest
    {
       NSString *response = [NSString stringWithContentsOfFile:
          [theRequest downloadDestinationPath] encoding:[theRequest responseEncoding] error:nil];
       // Note we're setting the baseURL to the url of the page we downloaded. This is important!
       [webView loadHTMLString:response baseURL:[request url]];
    }
    

    【讨论】:

    • 非常感谢这个示例,稍后再试。你能告诉我 ASIReplaceExternalResourcesWithData 如何将数据嵌入到页面中吗?该页面在缓存中仍然是一个 html 文件,对吗?请求是否修改了原来的html?
    • 是的,你是对的。 ASI 修改页面并将这些资产替换为Data URIs。或者,您可以使用称为 ASIReplaceExternalResourcesWithLocalURLs 的不同 URL 替换模式,它将资源作为文件下载,但仍修改 HTML 以指向这些本地文件。
    • 感谢 xoebus。我还发现要启用离线浏览,我需要添加这行代码:request.cachePolicy = ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy;
    猜你喜欢
    • 2012-05-16
    • 2011-11-24
    • 2013-05-17
    • 2012-09-15
    • 2014-07-19
    • 2021-11-28
    • 2016-07-26
    • 2014-03-01
    • 1970-01-01
    相关资源
    最近更新 更多