zoukankan      html  css  js  c++  java
  • 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=24*60*60*7;//一周
       
    
       
        
        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=24*60*60*7;//一周
       
        //设置完成时回调方法
        [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{
        
    }
  • 相关阅读:
    javascript定义类和实例化类
    c# 注册表操作,创建,删除,修改,判断节点是否存在
    asp.net 获得文件属性中的修改时间,获得系统文件属性的方法,最后一次写入时间
    研究“QQ开心农场”一点心得
    c# winform socket网络编程,点对点传输文件,socket文件传输,监听端口
    用C#创建Windows服务(Windows Services)
    c# webform js文件获取客户端控件,后台cs获取前台客户端控件的值,c#和js的交互
    c# asp.net webform web页面打印,可以控制需要打印和不需要打印的位置
    c# asp.net 调用系统设置字体文本框,设置label或页面字体,大小,FontDialog
    c# 计算程序执行时间,计算一段代码执行所用的时间,测试效率
  • 原文地址:https://www.cnblogs.com/gcb999/p/3238458.html
Copyright © 2011-2022 走看看