如何在iOS中使用图片caching整个网页

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

谢谢!

ASIHTTPRequest项目有一个名为ASIWebPageRequest的类,它被devise成完全按照你想要的来做。 如果您可以为您的项目添加额外的依赖项,那么我认为这是一个很好的解决scheme: 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]]; }