ASIHTTPRequest的使用——下载缓存
来源:互联网 发布:红警小说知乎 编辑:程序博客网 时间:2024/05/03 11:57
// .h文件#import <UIKit/UIKit.h>// 引导头文件#import "ASIHTTPRequest.h"#import "ASIDownloadCache.h"// 添加协议@interface ViewController : UIViewController <ASIHTTPRequestDelegate>- (void)click:(id)sender;@property (retain, nonatomic) UIImageView *img1;@end
// .m文件#import "ViewController.h"@interface ViewController ()@property (nonatomic, retain) NSDate *startData;@property (nonatomic, retain) NSDate *endData;@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (void)dealloc{ [_img1 release]; [self.startData release]; [self.endData release]; [super dealloc];}- (void)click:(id)sender{ [self download];}- (void)download{ // 第一种方法设置缓存 self.startData = [NSDate date]; NSLog(@"start-->%@",self.startData); // 1网络请求类 // 1-1 NSString *url = @"http://d.hiphotos.baidu.com/album/w%3D2048/sign=349954701b4c510faec4e51a5461272d/d1a20cf431adcbefc5d4f4beadaf2edda2cc9fa2.jpg"; __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]]; // 1-2 request.delegate = self; // 1-3设置缓存时间,一周 request.secondsToCache = 24 * 60 * 60 * 7; // 2下载缓存类 // 2-1 ASIDownloadCache *_cache = [ASIDownloadCache sharedCache]; // 2-2设置缓存目录 NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; cachePath = [cachePath stringByAppendingPathComponent:@"res"]; [_cache setStoragePath:cachePath]; // 2-3 [request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; [request setDownloadCache:_cache]; [request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy]; [request startAsynchronous]; // 第二种方法设置缓存 /* // 1网络请求类 // 1-1 NSString *url=@"http://f.hiphotos.baidu.com/album/w%3D2048/sign=288b34a9e4dde711e7d244f693d7cc1b/18d8bc3eb13533fa7f621782a9d3fd1f40345b42.jpg"; __block ASIHTTPRequest *request =[ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]]; // 1-2 request.delegate=self; // 1-3设置缓存时间,一周 request.secondsToCache = 24 * 60 * 60 * 7; // 1-4 设置完成时回调方法 [request setDidFinishSelector:@selector(success:)]; // 2下载缓存类 ASIDownloadCache *_cache = [ASIDownloadCache sharedCache]; // 2-1设置缓存目录 NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; cachePath = [cachePath stringByAppendingPathComponent:@"res"]; [_cache setStoragePath:cachePath]; // 2-2设置缓存策略 [_cache setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy]; // 2-3 request永久存储 [request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; [request setDownloadCache:_cache]; [request startAsynchronous]; */}#pragma mark - ASIHTTPRequestDelegate// 第二种方法设置缓存- (void)success:(ASIHTTPRequest *)request{ if ([request didUseCachedResponse]) { NSLog(@"来自于缓存"); } else { NSLog(@"NO"); }}- (void)requestFinished:(ASIHTTPRequest *)request{ if ([request didUseCachedResponse]) { self.endData = [NSDate date]; NSLog(@"来缓存-->%@",self.endData); self.img1.image = [UIImage imageWithData:[request responseData]]; } else { self.endData = [NSDate date]; NSLog(@" NO-->%@",self.endData); self.img1.image = [UIImage imageWithData:[request responseData]]; }}- (void)requestFailed:(ASIHTTPRequest *)request{ }@end
0 0
- ASIHTTPRequest的使用——下载缓存
- ASIHTTPRequest的使用——文件下载(带缓存)
- ASIHTTPRequest的使用—— ASIDownloadCache缓存
- ASIHTTPRequest的使用——下载文件
- ASIHTTPRequest的使用——网络请求缓存
- ASIHTTPRequest 设置下载缓存
- ASIHTTPRequest的使用——框架使用
- ASIHTTPRequest的使用——封装
- ASIHTTPRequest的使用——请求HTTPS
- ASIHTTPRequest的使用——介绍说明
- ASIHTTPRequest的使用——队列请求
- ASIHTTPRequest的使用—— NSHTTPCookie
- ASIHTTPRequest 缓存+下载文件到本地
- ASIHTTPRequest缓存策略的设置
- 使用ASIHTTPRequest实现下载功能
- ASIHTTPRequest的使用—— 超时代码判断
- ASIHTTPRequest的使用——ASIFormDataRequest(自定义封装类)
- ASIHTTPRequest的使用——同步或异步请求
- leetCode解题报告5道题(九)
- citrix pvs 在客户端安装完目标机器后,重启提示no entry found in database for device
- JavaScript(13)浏览器对象模型
- Linux用户认证机制
- ios判断nil、NULL和NSNull(null)
- ASIHTTPRequest的使用——下载缓存
- BlueTooth: BlueTooth的EDR是什么
- 32款iOS开发插件和工具介绍[效率]
- Determine the Position and Length of the Match (获取匹配的起始位置,终止位置,和长度)
- Ubuntu13.04下Iptables设置
- ASIHTTPRequest的使用——封装
- new/delete 和malloc/free 的区别一般汇总
- 面向对象与面向过程的区别
- 中美安迪影视并购CCI全面进军好莱坞