GET异步 请求图片步骤

- (IBAction)getImage:(id)sender {

//1,准备URL

NSString *str = @"http://e.hiphotos.baidu.com/image/h%3D1200%3Bcrop%3D0%2C0%2C1920%2C1200/sign=e13d66699825bc31345d059a6eefb6d2/6159252dd42a2834f85385ac5ab5c9ea14cebfa4.jpg";

NSURL *url = [NSURL URLWithString:str];

//2,创建请求对象

NSURLRequest *request = [NSURLRequest requestWithURL:url];

//链接对象,block 异步

__block MXTViewController *weakSelf = self;

NSOperationQueue *queue = [[NSOperationQueue new] autorelease];

[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

//获取data

UIImage *image = [UIImage imageWithData:data];

//回到主线程更新页面

dispatch_sync( dispatch_get_main_queue(), ^{

//block 不可以使用_imageView 的方式访问属性,会造成self 引用计数+1 ,不能使用强引用

//要使用weak

weakSelf.imageView.image = image;

});

}];

上一篇:getopt getopt_long


下一篇:用VS2013+VELT进行Linux开发