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缓存的使用分析,一起来 ...
随机推荐
- Binary Tree ZigZag Level Order Traversal leetcode java
题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from lef ...
- SQL多表连接查询(详细实例)(转)
http://www.xker.com/page/e2012/0708/117368.html select * from student,course where student.ID=course ...
- Install Hyper-V on Windows 10
Enable Hyper-V to create virtual machines on Windows 10.Hyper-V can be enabled in many ways includ ...
- mongoDB开发规范
mongoDB库的设计 mongodb数据库命名规范:db_xxxx 禁止使用任何 " _ "(下划线)外的特殊字符 禁止使用数字打头的库名 数据库名最多为 64字符 mongoD ...
- Windows xcopy
1.考虑下面的需求,把aaa目录下面的111.txt 拷贝到 bbb,如下:echo onxcopy .\aaa\111.txt .\bbb\ /ypause2.注意这里表示路径要用右斜杠,因为左斜杠 ...
- Discuz! X3安装第三方主题出现:对不起,您安装的不是正版应用..的解决方法
discuz 社区在更新到3.0以上后,增加了对插件的版本检测,在安装时,可能会出现:“对不起,您安装的不是正版应用,安装程序无法继续执行”的提示,要解决这个其实挺容易的,找到以下文件: /sourc ...
- Discuz常见小问题-如何取消登陆发帖验证码
1 正常情况下,用户点击登录之后,需要填写验证码 2 进入后台,点击防灌水,验证设置,然后下面的各个选项可以设置是否启用验证码.
- 【转】application.properties 常见配置
Various properties can be specified inside your application.properties/application.yml file or as co ...
- 【原】【BG】-一次虚拟化环境实践简要记录
部分涉及到Linux.Nginx.tomcat.MySQL等的点滴操作记录,时间长了,就忘掉了,偶尔整理一下操作的history,就此简要备份一下: [原][BG]-一次虚拟化环境实践简要记录: ht ...
- 1051: 手机(MOBILE)
#include <iostream> #include <iomanip> #include <cstdlib> #include <string> ...