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"];
        
    }
  • 相关阅读:
    redhat 6.7 telnet rpm 安装包
    linux下网络配置 命令
    修复南尼U盘
    mac获取root权限
    ubuntu二进制包安装openresty
    ubuntu18源码包安装openresty
    Python监控rabbitmq的代码
    win10不能将文件拖到另外一个程序中去的解决办法
    docker配置远程管理端口
    nginx的代理配置
  • 原文地址:https://www.cnblogs.com/gcb999/p/3238603.html
Copyright © 2011-2022 走看看