ios中asihttprequest 下载缓存
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 下载缓存的更多相关文章
- iOS中 断点下载详解 韩俊强的博客
布局如下: 基本拖拉属性: #import "ViewController.h" #import "AFNetworking.h" @interface Vie ...
- ios中网络请求缓存
#import <Foundation/Foundation.h> #import "ASIFormDataRequest.h" @protocol NetWorkde ...
- IOS中经典的缓存对比
http://bpoplauschi.wordpress.com/2014/03/21/ios-image-caching-sdwebimage-vs-fastimage/
- iOS UIImage DownLoad图片的下载缓存全部在此
iOS图片的下载缓存全部在此 分类: iOS编程 -- : 2075人阅读 评论() 收藏 举报 注意: 我的文章只写给自己看 ------------------------------------ ...
- iOS中 流媒体播放和下载 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 iOS中关于流媒体的简介:介于下载本地播放与实时流媒体之间的一种播放形式,下载本地播放必须全部将文件下载完成后才能播 ...
- ios中UIWebview和asiHttprequest的用法
原文地址为:http://www.cnblogs.com/pengyingh/articles/2343062.htmlasiHttprequest的用法 它对Get请求的响应数据进行缓存(被缓存的数 ...
- iOS利用SDWebImage图片下载缓存
一.我们先来了解一下SDWebImage的使用: 1.导入框架,引入头文件: #import "UIImageView+WebCache.h" 也可以直接使用CocoaPods来引 ...
- IOS中无缓存的图片载入
在IOS中,我们常用[UIImage imageNamed]方法获取图像,这种方法简便,容易理解.但是有个缺点,就是有缓存.这种方式 传人的图像的就是通过文件名方式文件名.如果,我们内存有限,我们就必 ...
- iOS中dyld缓存的实现原理是怎样的?
在iOS开发中,为了提升系统的安全性,很多系统库文件都被打包到一个缓存的文件当中即dyld缓存,那大家对dyld缓存了解多少呢?今天小编将和大家分享的就是一位iOS大神对dyld缓存的使用分析,一起来 ...
随机推荐
- webservice发布问题,部署iis后调用不成功
我使用的环境win8.vs2010.IIS8.0 vs2010中创建的webservice中的方法可以正常调用,但是发布到IIS上后,asmx文件能正常访问, 但是进入方法后, 点击 "调用 ...
- 揭秘uc浏览器二
这节,四个议题: ①一个网页显示在webview控件中 ②如何正常隐藏显示标题栏. ③如何用runnable来隐藏标题栏,这样子就更加的专业化. ④上节我们说道了QuickActionGrid,看他长 ...
- mahout安装
mahout是hadoop的一种高级应用.运行mahout需要提前安装好hadoop.hadoop的安装网上很多.而且也不复杂,这里不再讲述.这里默认hadoop已经安装完成. 1:下载二进制解压安装 ...
- [转]PostgreSQL Replication之扩展与BDR
原文:https://www.cnblogs.com/xmzzp/p/6284300.html postgres 实现master, slave ,且master是多主. -------------- ...
- [Canvas]New Running Dog
欲看效果请下载后用Chrome浏览器打开index.html观看,下载地址:https://files.cnblogs.com/files/xiandedanteng/51-NewRunningDog ...
- 加快AS的Gradle Build速度
随着项目的代码以及依赖库的日益增多,构建的速度却是越来越慢,Android Studio2.0 之后提供了Instant Run貌似福利来了,但经过一段时间的使用发现不尽如人意,最大的吐槽点是首次编译 ...
- Reshaper cannot resolve symbol
问题 不知道出了什么问题,在代码视图发现有些关键词显示为红色,并且Reshaper提示消息为Reshaper cannot resolve symbol XXX ,但编辑不会报错. 虽然不影响使用,但 ...
- ArcGIS10.4 Runtime Error R6034
现在甲方采购的ArcGIS Desktop正版,一般都是较高的版本(10.4或10.4.1),但10.4经常报出C++ Runtime R6034错误. 问题 "Microsoft Visu ...
- JavaScript 之 JavaScript 对象
重新看JavaScript对象,参考资料: RUNOOB.COM:http://www.runoob.com/jsref/jsref-fromcharcode.html: CodePlayer:htt ...
- IPC's epoch 6 is less than the last promised epoch 7
一.错误起因 Active NameNode日志出现异常IPC‘s epoch [X] is less than the last promised epoch [X+1],出现短期的双Active ...