最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

UIwebView实现html的离线缓存_html/css

来源:动视网 责编:小采 时间:2020-11-27 16:02:56
文档

UIwebView实现html的离线缓存_html/css

UIwebView实现html的离线缓存_html/css_WEB-ITnose:1、html的缓存主要采取ASIHTTPRequest的缓存策略 (1)、设置缓存策略 //设置缓存 ASIDownloadCache *cache=[[ASIDownloadCache alloc] init]; self.myCache=cache; //设置缓存路径 NSArray *paths =NSSearchPath
推荐度:
导读UIwebView实现html的离线缓存_html/css_WEB-ITnose:1、html的缓存主要采取ASIHTTPRequest的缓存策略 (1)、设置缓存策略 //设置缓存 ASIDownloadCache *cache=[[ASIDownloadCache alloc] init]; self.myCache=cache; //设置缓存路径 NSArray *paths =NSSearchPath
 1、html的缓存主要采取ASIHTTPRequest的缓存策略
(1)、设置缓存策略

 //设置缓存 ASIDownloadCache *cache=[[ASIDownloadCache alloc] init]; self.myCache=cache; //设置缓存路径 NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *documentDirectory = [paths objectAtIndex:0]; //设置缓存存放路径 [self.myCache setStoragePath:[documentDirectory stringByAppendingPathComponent:@"resource"]]; //ASIAskServerIfModifiedCachePolicy 与默认缓存大致一样,区别仅是每次请求都会 去服务器判断是否有更新 //ASIOnlyLoadIfNotCachedCachePolicy 如果有缓存在本地,不管其过期与否,总会拿来使用 //ASIFallbackToCacheIfLoadFailsCachePolicy 这个选项经常被用来与其它选项组合使用。请求失败时,如果有缓存当网络则返回本地缓存信息 [self.myCache setDefaultCachePolicy:ASIFallbackToCacheIfLoadFailsCachePolicy]; //设置缓存策略

(2)、设置异步缓存

 NSURL *Requesturl=[NSURL URLWithString:url]; ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:Requesturl]; // //获取全局变量 AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; // //设置缓存方式 [request setDownloadCache:appDelegate.myCache]; // //设置缓存数据存储策略,这里采取的是如果无更新或无法联网就读取缓存数据 [request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; [request setDelegate:self]; [request startAsynchronous];

html缓存完成。如想查看详细 请点击 : IOS开发网络篇之──ASIHTTPRequest详解

2、html中的图片缓存
(1)、通过正则获取html代码中的所有图片url

 NSString *urlPattern = @"]+?src=[\"']?([^>'\"]+)[\"']?"; NSError *error = [NSError new]; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:urlPattern options:NSRegularExpressionCaseInsensitive error:&error ]; //match 这块内容非常强大 NSUInteger counts =[regex numberOfMatchesInString:content options:NSRegularExpressionCaseInsensitive range:NSMakeRange(0, [content length])];//匹配到的次数 if(counts > 0){ NSArray* matches = [regex matchesInString:content options:NSMatchingReportCompletion range:NSMakeRange(0, [content length])]; for (NSTextCheckingResult *match in matches) { NSInteger count = [match numberOfRanges];//匹配项 for(NSInteger index = 0;index < count;index++){ NSRange halfRange = [match rangeAtIndex:index]; if (index == 1) { //[listImage addObject:[content substringWithRange:halfRange]]; NSLog(@"转换出来的字符串===%@",[content substringWithRange:halfRange]); [listImage addObject:[content substringWithRange:halfRange]]; } } }//遍历后可以看到三个range,1、为整体。2、为([\\w-]+\\.)匹配到的内容。3、([\\w.%&=-]*)匹配到的内容 }

(2)、SDwebImage下载图片 、JS交互替换

 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul); dispatch_group_t group = dispatch_group_create(); for (int i = 0; i < listImage.count; i++) { NSString *imageUrl = [imageUrlArray objectAtIndex:i]; NSString *key=[self getMd5_32Bit_String:imageUrl]; UIImage *cachedImage=[[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key]; NSString *index = [NSString stringWithFormat:@"%d", i]; if (cachedImage) { [tchWebView stringByEvaluatingJavaScriptFromString:[self createSetImageUrlJavaScript:index imgUrl:key]]; }else{ dispatch_group_async(group, queue, ^{ //异步下载图片 [SDWebImageDownloader.sharedDownloader downloadImageWithURL:[NSURL URLWithString:imageUrl] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) { // progression tracking code } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) { if (image && finished) { [[SDImageCache sharedImageCache] storeImage:image forKey:key]; dispatch_sync(dispatch_get_main_queue(), ^{ [tchWebView stringByEvaluatingJavaScriptFromString:[self createSetImageUrlJavaScript:index imgUrl:key]]; }); } }]; }); } } dispatch_release(group);
//设置下载完成的图片到web img- (NSString *)createSetImageUrlJavaScript:(NSString *) index imgUrl:(NSString *) url{ UIImage *myCachaImage=[[SDImageCache sharedImageCache] imageFromDiskCacheForKey:url]; NSData *imageData = UIImageJPEGRepresentation(myCachaImage,1.0); NSString *imageSource = [NSString stringWithFormat:@"data:image/jpg;base,%@",[imageData baseEncoding]]; NSString *js = [NSString stringWithFormat:@"var imgArray = document.getElementsByTagName('img'); imgArray[%@].src=\"%@\"; " , index, imageSource]; return js;}
//32位MD5加密方式- (NSString *)getMd5_32Bit_String:(NSString *)srcString{ const char *cStr = [srcString UTF8String]; unsigned char digest[CC_MD5_DIGEST_LENGTH]; CC_MD5( cStr, strlen(cStr), digest ); NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) [result appendFormat:@"%02x", digest[i]]; return result;}

参考:http://bbs.csdn.net/topics/390831054

文档

UIwebView实现html的离线缓存_html/css

UIwebView实现html的离线缓存_html/css_WEB-ITnose:1、html的缓存主要采取ASIHTTPRequest的缓存策略 (1)、设置缓存策略 //设置缓存 ASIDownloadCache *cache=[[ASIDownloadCache alloc] init]; self.myCache=cache; //设置缓存路径 NSArray *paths =NSSearchPath
推荐度:
标签: 缓存 it 实现
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top