優勢: 1 低耦合,易擴展。(這個下篇文章體現) 2.有緩存文件過時機制 默認一週(以最後緩存文件修改時間爲準) 3.顯示緩存大小/個數,所有清除緩存/單個文件清除緩存/按時間清除緩存 方法多樣 而且均可以自定義路徑 可擴展性強 4.離線下載功能 5.多種請求類型的判斷。也可不遵循,自由隨你定。git
/** 從新請求 ,不讀取緩存,從新請求*/
ZBRequestTypeRefresh,
/** 有緩存,讀取緩存 無緩存,從新請求*/
ZBRequestTypeCache,
/** 加載更多 ,不讀取緩存,從新請求*/
ZBRequestTypeRefreshMore,
/** 加載更多 ,有緩存,讀取緩存 無緩存,從新請求*/
ZBRequestTypeCacheMore,
/** 詳情 ,有緩存,讀取緩存 無緩存,從新請求*/
ZBRequestTypeDetailCache,
/** 自定義 ,有緩存,讀取緩存 無緩存,從新請求*/
ZBRequestTypeCustomCache
複製代碼
6.可見的緩存文件 github
AFNetworking 很少介紹了 在使用OC開發的,不少都用這個。封裝了一個緩存管理類ZBCacheManager,主要參照了SDWebImage的SDImageCache磁盤存儲的思路,SDWebImage的存儲性能你們應該不用懷疑,但也要進行大量的改動,畢竟SDImageCache 只是緩存圖片的。無論用那種請求方法 緩存文件都會存儲到沙盒 /Library/Caches/ZBKit/AppCache 路徑下 。 有一點要注意,請求下來的數據格式 是二進制的 由於緩存也是二進制數據 。因此設置了responseSerializer = [AFHTTPResponseSerializer serializer];不能更改。sql
請求示例 具體看demo 。 2017-8-21 重構了此庫 如今默認爲Refresh (從新請求,不讀緩存)。json
[ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
request.urlString=list_URL;
request.methodType=ZBMethodTypeGET;//默認爲GET
request.apiType=ZBRequestTypeCache;//默認爲ZBRequestTypeRefresh
} success:^(id responseObj,apiType type){
if (type==ZBRequestTypeRefresh) {
//下拉結束刷新
}
if (type==ZBRequestTypeLoadMore) {
// 上拉結束刷新
}
//請求成功
} failed:^(NSError *error){
if (error.code==NSURLErrorCancelled)return;
if (error.code==NSURLErrorTimedOut){
[self alertTitle:@"請求超時" andMessage:@""];
}else{
[self alertTitle:@"請求失敗" andMessage:@""];
}
}];
//取消對應的網絡請求
[ZBRequestManager cancelRequest: list_URL completion:^(NSString *urlString){
//若是請求成功 或 讀緩存 會返回null 沒法取消。請求未完成的會取消並返回對應url
//NSLog(@"取消對應url:%@ ",urlString);
}];
複製代碼
#離線緩存/下載api
//使用SDWebImage方法替換內部實現
- (void)downloadImages:(int)index {
[[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:_imageArray[index]] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize){
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType,BOOL finished,NSURL *imageURL){
if (image) {
self.images[index] = image;
//若是下載的圖片爲當前要顯示的圖片,直接到主線程給imageView賦值,不然要等到下一輪纔會顯示
if (_currIndex == index) [_currImageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
}
}];
}
複製代碼
好了 咱們的列表圖片包括輪播圖片 緩存 都在SDWebImage 的默認緩存路徑裏了,由於XRCarouselView 我並無徵得做者的贊成更改,因此demo中就不使用了,主要讓你們 知道 圖片的請求與存儲要統一 而咱們的json 數據 已經緩存到/Library/Caches/ZBKit/AppCache 路徑下 。下面開始 實現離線功能數組
思路就是 每一個頻道就是一個url,把你要緩存的 url 放到一個數組裏 進行遍歷請求。ZBRequestManager已經封裝好了 離線的方法緩存
- (void)requestOffline:(NSMutableArray *)offlineArray{
[ZBRequestManager sendBatchRequest:^(ZBBatchRequest *batchRequest){
for (NSString *urlString in offlineArray) {
ZBURLRequest *request=[[ZBURLRequest alloc]init];
request.urlString=urlString;
[batchRequest.urlArray addObject:request];
}
} success:^(id responseObj,apiType type){
NSLog(@"添加了幾個url請求 就會走幾遍");
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingMutableContainers error:nil];
NSArray *array=[dict objectForKey:@"videos"];
for (NSDictionary *dic in array) {
DetailsModel *model=[[DetailsModel alloc]init];
model.thumb=[dic objectForKey:@"thumb"]; //找到圖片的key
[self.imageArray addObject:model];
//使用SDWebImage 下載圖片
BOOL isKey=[[SDImageCache sharedImageCache]diskImageExistsWithKey:model.thumb];
if (isKey) {
self.offlineView.progressLabel.text=@"已經下載了";
} else{
[[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:model.thumb] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize){
NSLog(@"%@",[self progressStrWithSize:(double)receivedSize/expectedSize]);
self.offlineView.progressLabel.text=[self progressStrWithSize:(double)receivedSize/expectedSize];
self.offlineView.pv.progress =(double)receivedSize/expectedSize;
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType,BOOL finished,NSURL *imageURL){
NSLog(@"單個圖片下載完成");
self.offlineView.progressLabel.text=nil;
self.offlineView.progressLabel.text=[self progressStrWithSize:0.0];
self.offlineView.pv.progress = 0.0;
[self.tableView reloadData];
//讓 下載的url與模型的最後一個比較,若是相同證實下載完畢。
NSString *imageURLStr = [imageURL absoluteString];
NSString *lastImage=[NSString stringWithFormat:@"%@",((DetailsModel *)[self.imageArray lastObject]).thumb];
if ([imageURLStr isEqualToString:lastImage]) {
NSLog(@"下載完成");
[self.offlineView hide];//取消下載進度視圖
[self alertTitle:@"下載完成"andMessage:@""];
self.imageArray=nil;
}
if (error) {
NSLog(@"下載失敗");
}
}];
}
}
} failed:^(NSError *error){
if (error.code==NSURLErrorCancelled)return;
if (error.code==NSURLErrorTimedOut){
[self alertTitle:@"請求超時" andMessage:@""];
}else{
[self alertTitle:@"請求失敗" andMessage:@""];
}
}];
}
複製代碼
點擊 Github地址 下載bash
結尾:水平有限,代碼也很爛,一直在努力學習中,你們多多包涵。若是你喜歡這個輪子,請給個star,這是對做者最大的鼓勵和支持,拜謝!!!假如你有更好的想法或方案請留言!服務器
下篇文章:設計一個簡單的圖片緩存器 網絡