#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"]; }

ios中ASIHTTPRequst的封装的更多相关文章

  1. ios中tableview网封装(viewcontroller封装)常用的

    下载地址 http://pan.baidu.com/share/link?shareid=3657500168&uk=923776187 使用框架 1:asIHttpRequest库 2;SB ...

  2. iOS中 用FMDB封装一个SQLite数据库

    建立一个单例: DataBaseHandle.h #import <Foundation/Foundation.h> @class PersonModel; @class FMDataba ...

  3. QF——iOS中的数据库操作:SQLite数据库,第三方封装库FMDB,CoreData

    SQLite数据库: SQLite是轻量级的数据库,适合应用在移动设备和小型设备上,它的优点是轻量,可移植性强.但它的缺点是它的API是用C写的,不是面向对象的.整体来说,操作起来比较麻烦.所以,一般 ...

  4. 转载 -- iOS中SDK的简单封装与使用

    一.功能总述 在博客开始的第一部分,我们先来看一下我们最终要实现的效果.下图中所表述的就是我们今天博客中要做的事情,下方的App One和App Two都植入了我们将要封装的LoginSDK, 两个A ...

  5. iOS中数据库应用基础

    iOS 数据库入门 一.数据库简介 1.什么是数据库? 数据库(Database) 是按照数据结构来组织,存储和管理数据的仓库 数据库可以分为2大种类 关系型数据库(主流) PC端 Oracle My ...

  6. iOS 中的 HotFix 方案总结详解

    相信HotFix大家应该都很熟悉了,今天主要对于最近调研的一些方案做一些总结.iOS中的HotFix方案大致可以分为四种: WaxPatch(Alibaba) Dynamic Framework(Ap ...

  7. Quartz 2D在ios中的使用简述二:创建画布

    在iOS中使用Quartz画图时,第一步就是要获取画布(图形上下文),然后再画布上做各种操作.先看下CoreGraphics.h这个头文件,就可以知道能够创建多少种上下文类型. #include &l ...

  8. 谈谈iOS中的锁

    1 前言 近日工作不是太忙,刚好有时间了解一些其他东西,本来打算今天上午去体检,但是看看天气还是明天再去吧,也有很大一个原因:就是周六没有预约上!闲话少说,这里简单对锁来个简单介绍分享. 2 目录 第 ...

  9. iOS中的数据持久化方式

    iOS中的数据持久化方式,基本上有以下四种:属性列表.对象归档.SQLite3和Core Data. 1.属性列表 涉及到的主要类:NSUserDefaults,一般 [NSUserDefaults ...

随机推荐

  1. scrapy框架系列 (1) 初识scrapy

    Scrapy 框架 Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛. 框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页 ...

  2. Xamarin/Mono IOS Limitations

    http://developer.xamarin.com/guides/ios/advanced_topics/limitations/ Since applications on the iPhon ...

  3. OpenGL ES Shader语言中的函数不支持递归

    An example function definition is given here for a simple function that computes basic diffuse light ...

  4. 基于spring与mockito单元测试Mock对象注入

    转载:http://www.blogjava.net/qileilove/archive/2014/03/07/410713.html 1.关键词 单元测试.spring.mockito 2.概述 单 ...

  5. JAVA基础编程50题(4-6题)具体解释

    一.描写叙述 1.将一个正整数分解质因数.比如:输入90,打印出90=2*3*3*5. 程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完毕: (1)假设这个质数恰等于n,则说明 ...

  6. LeetCode Anagrams My solution

    Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs ...

  7. Linux网络编程:基于TCP的程序开发回顾篇《转》

    面向连接的TCP程序设计 基于TCP的程序开发分为服务器端和客户端两部分,常见的核心步骤和流程: 其实按照上面这个流程调用系统API确实可以完全实现应用层程序的开发,一点问题没有.可随着时间的推移,你 ...

  8. android中使用setOnKeyListener响应输入事件

    在界面中添加一个EditText输入框控件,需要在输入时响应输入事件,可以使用setOnKeyListener() 事件: final EditText editText = findViewById ...

  9. 【转】 Java多态特性:重载和覆写的比较

    Java重载: 在同一个类中 方法具有相同的名字,相同或不同的返回值,但参数不同的多个方法(参数个数或参数类型) public class MethoDemo{ public static void ...

  10. ES6 箭头函数(arrow function)

    例行声明:接下来的文字内容全部来自 Understanding ECMAScript 6,作者是Nicholas C.Zakas,也就是大名鼎鼎的Professional JavaScript for ...