// .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