您的位置:首页 > 其它

异步懒人加载图片(GET和POST请求方式)

2014-04-26 17:26 429 查看
异步加载图片

首先你要实现你的GET和POST请求方式 (遵循NSURLConnectionDataDelegate)

因为GET和POST的请求方式不一样但是他们的请求过程都是一样的

所以

我们需要写一个GET的请求方法只需要传入你的URLString就可以了

-(void)requestForGETWithUrl:(NSString *)urlString{

NSURL * url = [NSURL URLWithString:urlString];

NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

[request setHTTPMethod:@“GET”];

[NSURLConnection connectionWithRequest:request delegate:self];

}

还需要些一个POST的请求方式你需要传入你的URLString和你的具体的Data

- (void)requestForPOSTWithUrl:(NSString *)urlString postData:(NSData *)data

{

//封装URL对象

NSURL * url = [NSURL URLWithString:urlString];

//设置POST请求

NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

[request setHTTPMethod:@"POST"];

[request setHTTPBody:data];

//建立异步连接,使用设置delegate方式实现

self.connection = [NSURLConnection connectionWithRequest:request delegate:self];

最后就是实现你的delegate的四个方法在这些代理方法里面你能知道数据请求的状态 你可以做很多事情

你还要写一个自身的代理并且你得写三个你的代理执行的方法 是用来

- -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 在这个方法里面要创建一个空的可变的Data 这是开始解析了 在这里你可以获得你的数据的中长度NSHTTPURLResponse * httpResponse
= (NSHTTPURLResponse *)response 总长度 = httpResponse.expectedContentLength

httpResponse.statusCode这是状态码200表示是畅通无阻的

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data在这里是接受下载下来的数据时一段一段的接受 所以你要去一个一个的拼接[总的data appendData:data]在这里你可以获得下载的进度哦就是你现在接受的数据的长度必上总长度

- (void)connectionDidFinishLoading:(NSURLConnection *)connection在这里你可以返回你的总数据data 你可以接受这个data并且去解析,可以获得你想要的数据

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error在这里是失败信息

还应该有个取消请求的方法

因为你可能在点击时候又快速返回了你的数据 还在请求所以你要在你的View消失的时候要停止网络请求

所以你要定义一个NSURLConnection的属性来接受你的GET和POST请求的那个Connection

在你的上个一面消失的时候你执行你的-(void)cancelRequest[_connection cancel];并且你的self.connection = nil;因为你要保证下次请求的时候你的时空的

这些做完你要验证下你写的对不对

在这里传值的时候你要清晰的明白谁要传给谁 根据他的哪一行去找下载那一张图片

在封装的时候你要清楚你是先在本地中还是你要开始下载

这里的思维逻辑你是在你的展示的ViewController里面让其执行下载的方法 那么你就要先判断本地是不是有有的话就直接赋值没有再下载

定义一个下载类专门下载图片

在这个下载图片类里面你要明白是怎么吧你的值传过去的

//开始下载图片 -(void)startDownLoadImageWithImageUrl:(NSString *)imageUrl {



UIImage * image = [self locationImage:imageUrl];



if (image == Nil) {//判断本地有没有 //没有我就去下
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{



//子线程下载图像

NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];



//并且将下载的图片存到沙盒中

[data writeToFile:[self imageFilePath:imageUrl] atomically:YES];



//创建主线程去展示

dispatch_async(dispatch_get_main_queue(), ^{



UIImage * image = [UIImage imageWithData:data];



self.newsItem.newsImage = image;



if (_completionHandel) {//这里的_compltionHandel是生命的Block的属性((void)(^complitionHandel)(void))当你在回调 的时候你重写set方法 并且给你的cell上赋值



_completionHandel();



}



});



});



} }

//获取文件路径

-(NSString *)imageFilePath:(NSString *)imageFileUrlString {



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



NSString * imagePath = [cachesPath stringByAppendingPathComponent:@"Images"];



NSFileManager * fileManager = [NSFileManager defaultManager];



if (![fileManager fileExistsAtPath:imagePath]) {



[fileManager createDirectoryAtPath:imagePath withIntermediateDirectories:YES attributes:Nil error:Nil];



}



NSString * imageName = [imageFileUrlString stringByReplacingOccurrencesOfString:@"/" withString:@"-"];

NSString * imageNameFilePath = [imagePath stringByAppendingPathComponent:imageName];



return imageNameFilePath;

}



//获得本地的图片

-(UIImage *)locationImage:(NSString *)imageUrl {



NSString * imageFilePath = [self imageFilePath:imageUrl];



UIImage * image = [UIImage imageWithContentsOfFile:imageFilePath];



if (image) {



return image;



}



return Nil;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: