iOS 从网页缓存中获取图片

作者: Whde | 来源:发表于2016-02-19 17:32 被阅读971次

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

- (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;
}

相关文章

  • iOS 从网页缓存中获取图片

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

  • SDWebImage 从缓存中获取图片,当缓存中没有图片的时候

    SDWebImage 从缓存中获取图片,当缓存中没有图片的时候 会从网上获取图片 userdefalt中的数据是u...

  • Bitmap

    1.从应用中获取图片 2.从本地缓存中获取图片 3.将图片写入相册

  • iOS 小知识点总结

    1.iOS的UIImage的两种不同的图片加载方式 此种方式是直接加载图片,直接从文件中获取图片,不会出现缓存. ...

  • Java开源爬虫框架WebCollector图片抓取教程

    网站中的图片和网页在本质上是相同的,图片和网页的获取本质上都是根据URL从网站中获取网页/图片的字节数组(byte...

  • iOS 缓存之(WebView)网页缓存

    app中关于网页用的是越来越多了,所以有关网页缓存等问题就出现了?怎么进行缓存,获取缓存的大小,获取缓存数据,清理...

  • 缓存技术

    为了减少我们从网络获取数据(图片)的次数,我们会从网络获取到之后,缓存到我们的内存中。所以在我们网络编程中,缓存技...

  • NSURLRequest的缓存机制

    NSURLRequest可以实现网页缓存,下面实现了网页有更新时候,重新从网络获取,没有更新就使用本地缓存,如果没...

  • UIWebView网页缓存数据参考-iOS

    iOS中,关于UIWebView网页数据本地缓存原理和实际使用 UIWebview使用缓存并且保证实时性(iOS ...

  • Glide源码分析-加载任务

    DecodeJob任务 该任务获取图片,可以从资源中,文件缓存,或者网络中获取。下面看一下它的run方法。 该任务...

网友评论

    本文标题:iOS 从网页缓存中获取图片

    本文链接:https://www.haomeiwen.com/subject/lokvkttx.html