iOS边练边学--多线程NSOperation介绍,子类实现多线程的介绍(任务和队列),队列的取消、暂停(挂起)和恢复,操作依赖与线程间的通信

一、NSOperation

  • NSOperation和NSOperationQueue实现多线程的具体步骤
    • 先将需要执行的操作封装到一个NSOperation对象中
    • 然后将NSOperation对象添加到NSOperationQueue中
    • 系统会自动将NSOperationQueue中的NSOperation取出来
    • 将取出的NSOperation封装的操作放到一条新线程中执行
  • NSOperation是个抽象类,并不具备封装操作的能力,必须使用它的子类
  • 使用NSOperation子类的方式有3种
    • NSInvocationOperation
    • NSBlockOperation
    • 自定义子类继承NSOperation,实现内部相应的方法 -- 整理到了最后

二、NSOperation实现多线程的介绍(任务和队列)

  • <1>NSInvocationOperation

  • <2>NSBlockOperation

  • <3>NSOperationQueue

三、队列的取消、暂停(挂起)和恢复

四、操作依赖与线程间的通信 -- 练习:开启线程下载图片练习 和 开启线程下载两张图片后合成

 1 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
 2     NSOperationQueue *queue = [[NSOperationQueue alloc] init];
 3     
 4     __block UIImage *image1 = nil;
 5     // 下载图片1
 6     NSBlockOperation *download1 = [NSBlockOperation blockOperationWithBlock:^{
 7         
 8         // 图片的网络路径
 9         NSURL *url = [NSURL URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"];
10         
11         // 加载图片
12         NSData *data = [NSData dataWithContentsOfURL:url];
13         
14         // 生成图片
15         image1 = [UIImage imageWithData:data];
16     }];
17     
18     __block UIImage *image2 = nil;
19     // 下载图片2
20     NSBlockOperation *download2 = [NSBlockOperation blockOperationWithBlock:^{
21         
22         // 图片的网络路径
23         NSURL *url = [NSURL URLWithString:@"http://pic38.nipic.com/20140228/5571398_215900721128_2.jpg"];
24 
25         
26         // 加载图片
27         NSData *data = [NSData dataWithContentsOfURL:url];
28         
29         // 生成图片
30         image2 = [UIImage imageWithData:data];
31     }];
32     
33     // 合成图片
34     NSBlockOperation *combine = [NSBlockOperation blockOperationWithBlock:^{
35         // 开启新的图形上下文
36         UIGraphicsBeginImageContext(CGSizeMake(100, 100));
37         
38         // 绘制图片
39         [image1 drawInRect:CGRectMake(0, 0, 50, 100)];
40         image1 = nil;
41         
42         [image2 drawInRect:CGRectMake(50, 0, 50, 100)];
43         image2 = nil;
44         
45         // 取得上下文中的图片
46         UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
47         
48         // 结束上下文
49         UIGraphicsEndImageContext();
50         
51         // 回到主线程
52         [[NSOperationQueue mainQueue] addOperationWithBlock:^{
53             self.imageView.image = image;
54         }];
55     }];
56     [combine addDependency:download1];
57     [combine addDependency:download2];
58     
59     [queue addOperation:download1];
60     [queue addOperation:download2];
61     [queue addOperation:combine];
62 }
63 
64 /**
65  * 线程之间的通信
66  */
67 - (void)test
68 {
69     [[[NSOperationQueue alloc] init] addOperationWithBlock:^{
70         // 图片的网络路径
71        NSURL *url = [NSURL URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"];
72      
73         
74         // 加载图片
75         NSData *data = [NSData dataWithContentsOfURL:url];
76         
77         // 生成图片
78         UIImage *image = [UIImage imageWithData:data];
79         
80         // 回到主线程
81         [[NSOperationQueue mainQueue] addOperationWithBlock:^{
82             self.imageView.image = image;
83         }];
84     }];
85 }
View Code

五、自定义NSOperation

原文地址:https://www.cnblogs.com/gchlcc/p/5421217.html