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缓存的使用分析,一起来 ...
随机推荐
- 【BLE】CC2541之发现服务与特征值
一.简介 本文以SimpleBLECentral工程为例,解析CC2541作为主机时是如何发现从机的服务和特征值的. 二.实验平台 协议栈版本:BLE-CC254x-1.4.0 编译软件:IAR 8. ...
- c++字符串split 函数实现
- 经常遇到字符串分割问题,但是相对于c++而言实现比较麻烦,直接遍历一遍也很冗余 - 另外也适用于,在字符串中找到某个字符的所有位置 //函数功能:将输入字符串s,以字符串c(;)进行拆分,拆分结果 ...
- AS 阿里巴巴Java开发规约 CheckStyle-IDEA
Alibaba Java Coding Guidelines 简介 github地址:https://github.com/alibaba/p3c 官方文档 阿里巴巴Java开发手册(纪念版) ...
- HMM与分词、词性标注、命名实体识别
http://www.hankcs.com/nlp/hmm-and-segmentation-tagging-named-entity-recognition.html HMM(隐马尔可夫模型)是用来 ...
- Netflix推荐系统:从评分预测到消费者法则
http://in.sdo.com/?p=11 原文链接:Netflix recommendations: beyond the 5 stars (Part 1), (Part 2) 原文作者:Xav ...
- JAVA-错误The type BookServiceImpl must implement the inherited abstract method
错误原因 :是因为class继承了其他的类,没有导入过来,选择add unimplemented methods进行解决
- 四轴自适应控制算法的一些尝试开源我的山猫飞控和梯度在线辨识自适应等算法—(转)
本文的最主要目的在于抛砖引玉,阿莫论坛真的是非常好的一个论坛,没有这个论坛,没有那么多这个论坛上的前人无私的奉献和热烈的讨论,我想我是怎么也无法入门四轴的控制的.只是论坛上已经很多年都没有看到过新东西 ...
- linux 安装elasticsearch 可能遇到的问题
1.can not run elasticsearch as root 切换到非root用户 因为安全问题elasticsearch 不让用root用户直接运行,所以要创建新用户 第一步:liunx创 ...
- Android改动包名称规范方法
第一步.在项目上右键,选择android tools->rename application package,输入须要改为的名称,然后选择须要替换的文件里的包名.这里仅改动了project中包括 ...
- ImportError DLL load failed: %1 不是有效的 Win32 应用程序
操作系统:win7 64位,安装mysqldb 后提示:ImportError DLL load failed: %1 不是有效的 Win32 应用程序,是由于安装的32位的 MySQL-Python ...