美文网首页iOS多线程
4.5 NSOperation->3.0 线程间通信

4.5 NSOperation->3.0 线程间通信

作者: 蓝田_Loto | 来源:发表于2016-08-18 18:50 被阅读83次

本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正。


本文相关目录:
==================== 所属文集:4.0 多线程 ====================
4.1 多线程基础->1.0 进程 & 线程
······················ 2.0 多线程简介
4.2 pthread
4.3 NSThread->1.0 创建线程
····················· 2.0 线程属性
····················· 3.0 线程状态/线程生命周期
····················· 4.0 多线程安全隐患
····················· 5.0 线程间通讯和常用方法
4.4 GCD->1.0 GCD简介和使用
·············· 2.0 线程间的通信
·············· 3.0 其他用法
·············· 4.0 GCD 的定时器事件
4.5 NSOperation->1.0 NSOperation简介
························ 2.0 NSOperationQueue
························ 3.0 线程间通信
························ 4.0 自定义NSOperation
4.6 RunLoop - 运行循环
===================== 所属文集:4.0 多线程 =====================


图片下载示例:

#import "ViewController.h"

@interface ViewController ()
@property(weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  [self test2];
}

#pragma mark - 线程间通信(图片合成)
- (void)test1 {
  // 1.队列
  NSOperationQueue *queue = [[NSOperationQueue alloc] init];

  __block UIImage *image1 = nil;
  // 2.下载图片1
  NSBlockOperation *download1 = [NSBlockOperation blockOperationWithBlock:^{
    // 图片的网络路径
    NSURL *url =
        [NSURL URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/"
                             @"8/1/9981681/200910/11/1255259355826.jpg"];
    // 加载图片
    NSData *data = [NSData dataWithContentsOfURL:url];
    // 生成图片
    image1 = [UIImage imageWithData:data];
  }];

  __block UIImage *image2 = nil;
  // 3.下载图片2
  NSBlockOperation *download2 = [NSBlockOperation blockOperationWithBlock:^{
    // 图片的网络路径
    NSURL *url = [NSURL
        URLWithString:
            @"http://pic38.nipic.com/20140228/5571398_215900721128_2.jpg"];
    // 加载图片
    NSData *data = [NSData dataWithContentsOfURL:url];
    // 生成图片
    image2 = [UIImage imageWithData:data];
  }];

  // 4.合成图片
  NSBlockOperation *combine = [NSBlockOperation blockOperationWithBlock:^{
    // 开启新的图形上下文
    UIGraphicsBeginImageContext(CGSizeMake(100, 100));

    // 绘制图片1
    [image1 drawInRect:CGRectMake(0, 0, 50, 100)];
    image1 = nil;

    // 绘制图片2
    [image2 drawInRect:CGRectMake(50, 0, 50, 100)];
    image2 = nil;

    // 取得上下文中的图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    // 结束上下文
    UIGraphicsEndImageContext();

    // 5.回到主线程
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
      self.imageView.image = image;
    }];
  }];

  // 设置依赖操作
  [combine addDependency:download1];
  [combine addDependency:download2];

  //把操作添加到队列中
  [queue addOperation:download1];
  [queue addOperation:download2];
  [queue addOperation:combine];
}

#pragma mark - 线程间通信(图片下载)
- (void)test2 {
  [[[NSOperationQueue alloc] init] addOperationWithBlock:^{
    // 图片的网络路径
    NSURL *url =
        [NSURL URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/"
                             @"8/1/9981681/200910/11/1255259355826.jpg"];

    // 加载图片
    NSData *data = [NSData dataWithContentsOfURL:url];

    // 生成图片
    UIImage *image = [UIImage imageWithData:data];

    // 回到主线程
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
      self.imageView.image = image;
    }];
  }];
}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

@end

本文源码 Demo 详见 Github
https://github.com/shorfng/iOS_4.0_multithreading.git




作者:蓝田(Loto)
出处: 简书

如果你觉得本篇文章对你有所帮助,请点击文章末尾下方“喜欢”
如有疑问,请通过以下方式交流:
评论区回复微信(加好友请注明“简书+称呼”)发送邮件shorfng@126.com



本文版权归作者和本网站共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

相关文章

  • 4.5 NSOperation->3.0 线程间通信

    本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正。 本文相关目...

  • EventBus 3.0使用

    EventBus可以实现组件间通信,线程通信,比较方便灵活,2.x和3.0差别很多。 Android Studio...

  • ios 多线程的故事4

    线程间通信 线程间通信:在1个进程中,线程往往不是孤立存在的,多个线程之间需要经常进行通信 线程间通信的体现 1个...

  • Android 面试常问知识

    Q1:线程间的通信进程间通信的几种方式进程间通信方式详解Q2:线程安全SharePreferences 是否线程安...

  • iOS进程间通信

    线程间通信 :通过performSelector系列的方法 可以实现 各种线程间的通信(通信 :调用与传参)进程间...

  • 线程间通信

    线程间通信就是子线程和主线程之间的通信

  • 《iOS高级开发之多线程编程之二》

    线程间的通信 在一个进程中,线程往往不是孤立存在的,多个线程之间经常进行通信,称为线程间通信。 NSThread ...

  • 2.Java内存模型

    1.java并发编程的线程间通信及线程间如何同步线程间通信分为:共享内存,消息传递。线程间同步:共享内存是代码指定...

  • 多线程之iOS线程间通信

    什么叫做线程间通信在1个进程中,线程往往不是孤立存在的,多个线程之间需要经常进行通信 线程间通信的体现 1个线程传...

  • 8.2 线程通信

    线程通信 简介:线程间通信是指多个线程间等待与唤醒的一个交互; 1.JDK5之前传统线程的通信方式,使用...

网友评论

    本文标题:4.5 NSOperation->3.0 线程间通信

    本文链接:https://www.haomeiwen.com/subject/ugzalttx.html