如何在 iOS 中缓存带有图像的整个网页 [英] how to cache a whole web page with images in iOS

查看:30
本文介绍了如何在 iOS 中缓存带有图像的整个网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on a image-text mixture page on iOS. I know it is possible for me to use UIWebView to achieve the goal. But the problem is, user may need to read the page offline. For the text part, I can save the html to the disk and load it in the offline mode. But how about images? Is it possible to cache the images to the disk and the UIWebView can still be able to display them?

谢谢!

推荐答案

ASIHTTPRequest 项目 有一个名为 的类ASIWebPageRequest 旨在完全满足您的需求.如果您同意向项目添加额外的依赖项,那么我认为这对您来说是一个很好的解决方案:ASIWebPageRequest.

The ASIHTTPRequest project has a class called ASIWebPageRequest which is designed to do exactly what you want. If you're okay with adding an additional dependency to your project then I think it's a good solution for you: ASIWebPageRequest.

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

On the page I liked above there are some good examples of how to use it but I'll include one of them here for completeness:

- (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]];
}

这篇关于如何在 iOS 中缓存带有图像的整个网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆