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. go语言之进阶篇主协程先退出

    1.主协程先退出 示例: package main import ( "fmt" "time" ) //主协程退出了,其它子协程也要跟着退出 func main ...

  2. go语言之进阶篇recover的使用

    1.recover的使用 示例: package main import "fmt" func testa() { fmt.Println("aaaaaaaaaaaaaa ...

  3. [leetcode]Word Break @ Python

    原题地址:https://oj.leetcode.com/problems/word-break/ 题意: Given a string s and a dictionary of words dic ...

  4. JQuery之ContextMenu(右键菜单)

    插件下载地址:http://www.trendskitchens.co.nz/jquery/contextmenu/jquery.contextmenu.r2.js压缩版:http://www.tre ...

  5. ODBC更新记录集提示”记录集为只读“

    创建的ODBC应用程序默认的记录集不具有只读属性,但是再更新记录表时会提示”记录集为只读“,这是为什么呢? 今天看书找到了答案: 因为MFC中的数据库类不支持需要连接两个或者多个表的记录集更新,如果选 ...

  6. python3 文件及文件夹路径相关

    1. #返回当前文件所在的目录 currentDir = path.dirname(__file__) # __file__ 为当前文件 2.获得某个路径的父级目录: parent_path = os ...

  7. 如何把本地代码提交到git(码云)、github代码管理项目上

    前提必须安装了git工具 以下命令操作都是通过git bash操作. 将本地代码提交到码云空项目操作: D:\>mkdir inek_winprop D:\>cd inek_winprop ...

  8. 关于Promise的一些个人理解jQuery的deferred

      一.什么是deferred对象? 开发网站的过程中,我们经常遇到某些耗时很长的javascript操作.其中,既有异步的操作(比如ajax读取服务器数据),也有同步的操作(比如遍历一个大型数组), ...

  9. 生日日期联动选择birthday.js

    实例下载

  10. PAT 1069 1070 1071 1072

    pat 1069 The Black Hole of Numbers 水题,代码如下: #include<cstdio> #include<cstdlib> #include& ...