标签:
1 #pragma mark NSBlockOperation和 NSOperationQueue 的搭配
2
3 - (void)viewDidLoad {
4
5 [super viewDidLoad];
6
7 // 1、创建视图
8
9 imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 200, 200)];
10
11 [self.view addSubview:imageView];
12
13 // 2、创建一个线程操作
14
15 NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
16
17
18
19 // 5、加载网络资源
20
21 NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kurl]];
22
23
24
25 UIImage *image = [UIImage imageWithData:data];
26
27
28
29 // 6、返回主线程
30
31 [[NSOperationQueue mainQueue]addOperationWithBlock:^{
32
33
34
35 // 7、在主线程更新UI
36
37 imageView.image =image;
38
39 }];
40
41 }];
42
43 // 3、创建一个线程队列
44
45 NSOperationQueue *operationQueue = [NSOperationQueue new];
46
47 // 4、把线程操作放到线程操作队列里
48
49 [operationQueue addOperation:blockOperation];
50
51 }
52
53 @end
标签:
原文地址:http://www.cnblogs.com/liuzhi20101016/p/5239443.html