從網(wǎng)頁緩存中獲取圖片,將圖片存放當(dāng)本地文件夾中
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSArray *a=[self getImgs];
for (NSString *str in a) {
NSURLCache *sharedCache = (NSURLCache *)[NSURLCache sharedURLCache];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:str]];
__block NSString *wstr = str;
[sharedCache getCachedResponseForDataTask:task completionHandler:^(NSCachedURLResponse * _Nullable cachedResponse) {
NSString *path = [NSString stringWithFormat:@"/Users/whde/Desktop/t/%@", [wstr lastPathComponent]];
[cachedResponse.data writeToFile:path options:NSDataWritingAtomic error:nil];
}];
}
}
/// 獲取所有圖片鏈接
- (NSArray *)getImgs
{
NSMutableArray *arrImgURL = [[NSMutableArray alloc] init];
for (int i = 0; i < [self nodeCountOfTag:@"img"]; i++) {
NSString *jsString = [NSString stringWithFormat:@"document.getElementsByTagName('img')[%d].src", i];
[arrImgURL addObject:[webView stringByEvaluatingJavaScriptFromString:jsString]];
}
return arrImgURL;
}
/// 獲取某個標(biāo)簽的結(jié)點個數(shù)
- (int)nodeCountOfTag:(NSString *)tag
{
NSString *jsString = [NSString stringWithFormat:@"document.getElementsByTagName('%@').length", tag];
int len = [[webView stringByEvaluatingJavaScriptFromString:jsString] intValue];
return len;
}