//同步下载
+(UIImage *)imageDownLoadWithURLString:(NSString *)iamgeURL{
//根据传递进来的网址字符串创建请求对象
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:iamgeURL]];
//使用data 对象接受请求同步的图片数据
NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
UIImage *image=[UIImage imageWithData:data];
//返回图片对象
return image;
}
//异步下载
+(void)imageLoadWithURLString:(NSString *)iamgeURL delegate:(id<ZGC_ImageDownLoad>)delegate{
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:iamgeURL]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
UIImage *image=[UIImage imageWithData:data];
//如果代理对象不为空,且实现了协议方法,代理执行此方法(传递值)
if (delegate!=nil && [delegate respondsToSelector:@selector(iamgeDownLoadImage:)]) {
[delegate iamgeDownLoadImage:image];
}
}];
}
+(void)downLoadimage:(NSString *)imageURL block:(ImageDownLoadBlock)block{
//根据图片网址字符串创建请求对象
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:imageURL]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
UIImage *image=[UIImage imageWithData:data];
//传值
block(image);
}];
}