从网页缓存中获取图片,将图片存放当本地文件夹中

来源:互联网 发布:sftp 修改端口号 编辑:程序博客网 时间:2024/05/17 22:28
- (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;}///  获取某个标签的结点个数- (int)nodeCountOfTag:(NSString *)tag{    NSString *jsString = [NSString stringWithFormat:@"document.getElementsByTagName('%@').length", tag];    int len = [[webView stringByEvaluatingJavaScriptFromString:jsString] intValue];    return len;}

0 0