ASIHTTPRequest的使用——封装

// .h文件
#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

// .m文件
#import "AsyncDownLoading.h"

@implementation AsyncDownLoading

- (void)dealloc
{
    Block_release(self.imageBlock);
    [super dealloc];
}

+ (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];
    }
}

@end

//使用
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"];









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值