多线程上传 图片

 8 
 9 #import "YYViewController.h"
10 
11 @interface YYViewController ()
12 @property (weak, nonatomic) IBOutlet UIImageView *imageView;
13 
14 @end
15 
16 @implementation YYViewController
17 
18 - (void)viewDidLoad
19 {
20     [super viewDidLoad];
21     
22 }
23 
24 //当手指触摸屏幕的时候,从网络上下载一张图片到控制器的view上显示
25 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
26 {
27     
28     //1.获取一个全局串行队列
29     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
30     //2.把任务添加到队列中执行
31     dispatch_async(queue, ^{
32         
33         //打印当前线程
34         NSLog(@"%@",[NSThread currentThread]);
35       //3.从网络上下载图片
36         NSURL *urlstr=[NSURL URLWithString:@"http://h.hiphotos.baidu.com/baike/w%3D268/sign=30b3fb747b310a55c424d9f28f444387/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg"];
37         NSData *data=[NSData dataWithContentsOfURL:urlstr];
38         UIImage *image=[UIImage imageWithData:data];
39         //提示
40         NSLog(@"图片加载完毕");
41         
42         //4.回到主线程,展示图片
43 //        [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
44         dispatch_async(dispatch_get_main_queue(), ^{
45             self.imageView.image=image;
46             //打印当前线程
47             NSLog(@"%@",[NSThread currentThread]);
48         });
49     });
50 }
51 
52 @end

你可能感兴趣的:(多线程上传 图片)