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{
        
    }
  • 相关阅读:
    Java进阶知识查漏补缺05
    Java进阶知识查漏补缺04
    Java进阶知识查漏补缺03
    Java进阶知识查漏补缺02
    Java进阶知识查漏补缺01
    在IDEA下导入项目后,WEB网页只显示jsp源码的解决方法
    Tutorial 3_工厂方法模式
    Tutorial 2_简单工厂模式
    Tutorial 1_UML与面向对象程序设计基本原则
    Java知识查漏补缺-04
  • 原文地址:https://www.cnblogs.com/gcb999/p/3238458.html
Copyright © 2011-2022 走看看