保存webView加载出来的图片至本地沙盒

- (void)webViewDidFinishLoad:(UIWebView*)webView{

NSArray*a=[selfgetAllImageLink];

for(NSString*strina) {

NSLog(@"图片网址%@",str);

NSURLCache*sharedCache = (NSURLCache*)[NSURLCachesharedURLCache];

NSURLSession*session = [NSURLSessionsharedSession];

NSURLSessionDataTask*task = [sessiondataTaskWithURL:[NSURLURLWithString:str]];

__blockNSString*wstr = str;

[sharedCachegetCachedResponseForDataTask:taskcompletionHandler:^(NSCachedURLResponse*_NullablecachedResponse) {

//在这里经图片保存进沙盒中SDWeb路径下

//NSString *path = [NSString stringWithFormat:@"/Users/xiaoyuliu/Desktop/abc/%@", [wstr lastPathComponent]];

//[cachedResponse.data writeToFile:path options:NSDataWritingAtomic error:nil];

}];

}

}

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {

returnYES;

}

///获取所有图片链接

- (NSArray*)getAllImageLink

{

NSMutableArray*arrImgURL = [[NSMutableArrayalloc]init];

for(inti =0; i < [selfnodeCountOfTag:@"img"]; i++) {

NSString*jsString = [NSStringstringWithFormat:@"document.getElementsByTagName('img')[%d].src", i];

[arrImgURLaddObject:[self.webViewstringByEvaluatingJavaScriptFromString:jsString]];

}

returnarrImgURL;

}

///获取某个标签的结点个数

- (int)nodeCountOfTag:(NSString*)tag

{

NSString*jsString = [NSStringstringWithFormat:@"document.getElementsByTagName('%@').length", tag];

intlen = [[self.webViewstringByEvaluatingJavaScriptFromString:jsString]intValue];

returnlen;

}

你可能感兴趣的:(保存webView加载出来的图片至本地沙盒)