iOS html图片缓存,如何在iOS中缓存带有图像的整个网页

xoebus..

15

该ASIHTTPRequest项目有一个叫做类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]];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值