美文网首页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 线程间通信

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