Uiimage+ImageDownLoad.h
#import <UIKit/UIKit.h>
typedef void(^ImageBlock) (UIImage *img);
@interface UIImage (ImageDownLoad)
+(UIImage *)imageDownLoadWithURL:(NSString *)urlString myBlock:(ImageBlock)mb;
@end
UIImage+ImageDownLoad.m
#import "UIImage+ImageDownLoad.h"
@implementation UIImage (ImageDownLoad)
+(UIImage *)imageDownLoadWithURL:(NSString *)urlString myBlock:(ImageBlock)mb{
NSURL *url=[NSURL URLWithString:urlString];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
UIImage *img=[UIImage imageWithData:data];
//用来图片下载完成,显示图片
mb(img);
} ];
return [UIImage imageNamed:@"87.png"];
}
@end
控制器里边调用
__weak CWViewController *weakSelf=self;
self.imv.image=[UIImage imageDownLoadWithURL:@"http://g.hiphotos.baidu.com/image/pic/item/c8ea15ce36d3d539f80e33f23987e950352ab0cb.jpg" myBlock:^(UIImage *m) {
weakSelf.imv.image=m;
}];
}