ASIHTTPRequest的使用——下载缓存

// .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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值