简体   繁体   English

如何使用 iOS 中的图像缓存整个 web 页面

[英]how to cache a whole web page with images in iOS

I am working on a image-text mixture page on iOS.我正在处理 iOS 上的图像-文本混合页面。 I know it is possible for me to use UIWebView to achieve the goal.我知道我可以使用 UIWebView 来实现目标。 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.对于文本部分,我可以将 html 保存到磁盘并以离线模式加载。 But how about images?但是图像呢? Is it possible to cache the images to the disk and the UIWebView can still be able to display them?是否可以将图像缓存到磁盘并且 UIWebView 仍然可以显示它们?

Thank you!谢谢!

The ASIHTTPRequest project has a class called ASIWebPageRequest which is designed to do exactly what you want. ASIHTTPRequest 项目有一个名为ASIWebPageRequest的 class,它旨在完全按照您的意愿行事。 If you're okay with adding an additional dependency to your project then I think it's a good solution for you: ASIWebPageRequest .如果您可以在项目中添加额外的依赖项,那么我认为这对您来说是一个很好的解决方案: 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]];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM