zoukankan      html  css  js  c++  java
  • ios中ASIHTTPRequst的封装

    #import <Foundation/Foundation.h>
    #import "ASIHTTPRequest.h"
    #import "ASIDownloadCache.h"
    typedef void (^MyBlock)(UIImage *image);
    
    @interface AsyncDownLoading : NSObject
    +(id)ShareAsyncDownload;
    -(void)LoadImageWithUrl:(NSString *)url;
    @property(nonatomic,copy)MyBlock imageBlock;
    @end

    #import "AsyncDownLoading.h"

    
    

     

    
    

    @implementation AsyncDownLoading

    
    

     

    
    

    +(id)ShareAsyncDownload{

    
    

        static dispatch_once_t onceToken;

    
    

        static AsyncDownLoading *downLoad=nil;

    
    

        dispatch_once(&onceToken, ^{

    
    

            downLoad=[[AsyncDownLoadingalloc] init];

    
    

        });

    
    

        return downLoad;

    
    

    }

    
    

    //暂时没有用

    
    

    -(NSString *)cacheFileForImage:(NSString *)imageUrl{

    
    

        NSString *cacheFolder=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

    
    

        cacheFolder=[cacheFolder stringByAppendingPathComponent:@"imagecache"];

    
    

        NSFileManager *fmgr=[NSFileManagerdefaultManager];

    
    

        if (![fmgr fileExistsAtPath:cacheFolder]) {

    
    

            [fmgr createDirectoryAtPath:cacheFolder withIntermediateDirectories:YESattributes:nilerror:nil];

    
    

        }

    
    

        NSArray *paths=[imageUrl componentsSeparatedByString:@"/"];

    
    

       NSString *saveStr= [[paths lastObject] length]>0?[paths lastObject]:@"";

    
    

        return [NSString stringWithFormat:@"%@/%@",cacheFolder,saveStr];

    
    

        

    
    

    }

    
    

     

    
    

    -(void)LoadImageWithUrl:(NSString *)url{

    
    

        __block ASIHTTPRequest *Request=nil;

    
    

            if(url){

    
    

                Request=[ASIHTTPRequestrequestWithURL:[NSURLURLWithString:url]];

    
    

                [Request setDelegate: self];

    
    

            //设置下载缓存

    
    

            [Request setDownloadCache:[ASIDownloadCachesharedCache]];

    
    

            // 设置缓存策略

    
    

            [Request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];

    
    

            //设置指定存储方式

    
    

            [Request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];

    
    

     

    
    

            //在后台也运行

    
    

            [Request setShouldContinueWhenAppEntersBackground:YES];

    
    

            [Request setTimeOutSeconds:60];

    
    

     

    
    

            [Request setCompletionBlock:^{

    
    

            if([Request didUseCachedResponse]){

    
    

            NSLog(@"缓存");

    
    

            }

    
    

            else{

    
    

            NSLog(@"重新创建");

    
    

            }

    
    

                NSLog(@"--1--%@",[NSThreadcurrentThread]);

    
    

            UIImage *image=[UIImageimageWithData:[Request responseData]];

    
    

            self.imageBlock(image);

    
    

            }];

    
    

            [Request setFailedBlock:^{

    
    

     

    
    

            NSLog(@"请求失败error-->%@",Request.error.localizedDescription);

    
    

        

    
    

            }];

    
    

                [Request startAsynchronous];

    
    

    }

    
    

    }

    
    

    -(void)dealloc

    
    

    {

    
    

        Block_release(self.imageBlock);

    
    

        [super dealloc];

    
    

    }

    
    

    @end

     
    - (IBAction)clicki2:(id)sender {
        AsyncDownLoading *down=[AsyncDownLoading ShareAsyncDownload];
        down.imageBlock=^(UIImage *image){
            self.img1.image=image;
        };
        [down LoadImageWithUrl:@"http://h.hiphotos.baidu.com/album/w%3D2048/sign=45753fa7e850352ab1612208677bfaf2/2e2eb9389b504fc2aae48015e4dde71190ef6d62.jpg"];
        
    }
  • 相关阅读:
    js实现深拷贝的几种方法
    禁止浏览器的默认行为 图片拖动 复制 剪切 右击
    祈祷奇迹,其实不如无尽的练习
    洛谷P4643 [国家集训队]阿狸和桃子的游戏 & 初赛心情
    Re:prime 关于质数的算法
    【洛谷有题】NOIP 2014 提高组初赛试题 订正 网络协议 检索/比较次数计算
    补码的快速计算
    2020洛谷初赛模拟 订正
    【洛谷有题】NOI 笔试题库(非初赛)订正
    树状数组小结
  • 原文地址:https://www.cnblogs.com/gcb999/p/3238603.html
Copyright © 2011-2022 走看看