asi中下载缓存第一种方法

#import <UIKit/UIKit.h>
#import "ASIHTTPRequest.h"
#import "ASIDownloadCache.h"
@interface ViewController : UIViewController<ASIHTTPRequestDelegate>
- (IBAction)click:(id)sender;
@property (retain, nonatomic) IBOutlet UIImageView *img1; @end #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.
} - (IBAction)click:(id)sender { [self download]; } -(void)download{
self.startData=[NSDate date];
NSLog(@"start-->%@",self.startData);
__block ASIHTTPRequest *request=nil;
NSString *url=@"http://d.hiphotos.baidu.com/album/w%3D2048/sign=349954701b4c510faec4e51a5461272d/d1a20cf431adcbefc5d4f4beadaf2edda2cc9fa2.jpg";
request=[ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
request.delegate=self;
//设置缓存时间
request.secondsToCache=***;//一周 NSString *cachePath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
cachePath =[cachePath stringByAppendingPathComponent:@"res"];
//缓存类
ASIDownloadCache *_cache=[ASIDownloadCache sharedCache]; [request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setDownloadCache:_cache];
[request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
[request startAsynchronous]; } -(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{ } - (void)dealloc {
[_img1 release];
[self.startData release];
[self.endData release];
[super dealloc];
}
@end

第二设置缓存

-(void)download{
__block ASIHTTPRequest *request=nil; NSString *url=@"http://f.hiphotos.baidu.com/album/w%3D2048/sign=288b34a9e4dde711e7d244f693d7cc1b/18d8bc3eb13533fa7f621782a9d3fd1f40345b42.jpg";
request=[ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
request.delegate=self;
//设置缓存时间
request.secondsToCache=***;//一周 //设置完成时回调方法
[request setDidFinishSelector:@selector(success:)]; NSString *cachePath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
cachePath =[cachePath stringByAppendingPathComponent:@"res"];
//缓存类
ASIDownloadCache *_cache=[ASIDownloadCache sharedCache];
//设置缓存目录
[_cache setStoragePath:cachePath];
//设置缓存策略
[_cache setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy]; //request永久存储
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setDownloadCache:_cache]; [request startAsynchronous];
} -(void)success:(ASIHTTPRequest *)request{
if ([request didUseCachedResponse]) {
NSLog(@"来自于缓存");
}
else{
NSLog(@"NO");
}
} -(void)requestFailed:(ASIHTTPRequest *)request{ }

ios中asihttprequest 下载缓存的更多相关文章

  1. iOS中 断点下载详解 韩俊强的博客

    布局如下: 基本拖拉属性: #import "ViewController.h" #import "AFNetworking.h" @interface Vie ...

  2. ios中网络请求缓存

    #import <Foundation/Foundation.h> #import "ASIFormDataRequest.h" @protocol NetWorkde ...

  3. IOS中经典的缓存对比

    http://bpoplauschi.wordpress.com/2014/03/21/ios-image-caching-sdwebimage-vs-fastimage/

  4. iOS UIImage DownLoad图片的下载缓存全部在此

    iOS图片的下载缓存全部在此 分类: iOS编程 -- : 2075人阅读 评论() 收藏 举报 注意: 我的文章只写给自己看 ------------------------------------ ...

  5. iOS中 流媒体播放和下载 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 iOS中关于流媒体的简介:介于下载本地播放与实时流媒体之间的一种播放形式,下载本地播放必须全部将文件下载完成后才能播 ...

  6. ios中UIWebview和asiHttprequest的用法

    原文地址为:http://www.cnblogs.com/pengyingh/articles/2343062.htmlasiHttprequest的用法 它对Get请求的响应数据进行缓存(被缓存的数 ...

  7. iOS利用SDWebImage图片下载缓存

    一.我们先来了解一下SDWebImage的使用: 1.导入框架,引入头文件: #import "UIImageView+WebCache.h" 也可以直接使用CocoaPods来引 ...

  8. IOS中无缓存的图片载入

    在IOS中,我们常用[UIImage imageNamed]方法获取图像,这种方法简便,容易理解.但是有个缺点,就是有缓存.这种方式 传人的图像的就是通过文件名方式文件名.如果,我们内存有限,我们就必 ...

  9. iOS中dyld缓存的实现原理是怎样的?

    在iOS开发中,为了提升系统的安全性,很多系统库文件都被打包到一个缓存的文件当中即dyld缓存,那大家对dyld缓存了解多少呢?今天小编将和大家分享的就是一位iOS大神对dyld缓存的使用分析,一起来 ...

随机推荐

  1. 算法-强连通分量和Kosaraju算法

    有向图中,连通性比较好理解,如果两个顶点V和顶点W是可达的,可以称之为强连通的,即存在路径A→B,同时也存在一条有向路径B→A.从之前的有向环的判定过程中其实我们可以得到一个结论就是两个是强连通的当且 ...

  2. (十一) 整合spring cloud云架构 - SSO单点登录之OAuth2.0登录流程(2)

    上一篇是站在巨人的肩膀上去研究OAuth2.0,也是为了快速帮助大家认识OAuth2.0,闲话少说,我根据框架中OAuth2.0的使用总结,画了一个简单的流程图(根据用户名+密码实现OAuth2.0的 ...

  3. background: inherit制作倒影、单行居中两行居左超过两行省略

    1.background: inherit;制作倒影 方法很多,但是我们当然要寻找最快最便捷的方法,至少得是无论图片怎么变化,div 大小怎么变化,我们都不用去改我们的代码. -webkit-box- ...

  4. 使用新版SDK不想跳转微博客户端能否直接发送微博分享?

    如题啊如题! 新版本中没有StatusesAPI?????

  5. Pandas Series笔记

    1.指向单元素的,类型为元素的类型 2.指向多个元素的,类型为Series 3.如果用索引切片,索引是非整型的,实际按照索引顺序取值,且包含末端 4.如果指定的索引不存在,则会报错 5.可以根据元素的 ...

  6. Direct2D教程VI——转换(Transform)

    目前博客园中成系列的Direct2D的教程有 1.万一的 Direct2D 系列,用的是Delphi 2009 2.zdd的 Direct2D 系列,用的是VS中的C++ 3.本文所在的 Direct ...

  7. Android中MVP模式与MVC模式比較(含演示样例)

    原文链接 http://sparkyuan.me/ 转载请注明出处 MVP 介绍 MVP模式(Model-View-Presenter)是MVC模式的一个衍生. 主要目的是为了解耦,使项目易于维护. ...

  8. PHP http_build_query()方法

    http_build_query (PHP 5) http_build_query -- 生成 url-encoded 之后的请求字符串描述 string http_build_query ( arr ...

  9. Android 如何关闭Navigation Bar M

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...

  10. spring boot 运行提示:Process finished with exit code 1

    spring boot 运行提示:Process finished with exit code 1 经检查发现是由于在application.properties配置文件中将某些自定义配置项移除了, ...