zoukankan      html  css  js  c++  java
  • iOS----线程之间的通信

    当线程的数量大于一个的时候,线程之间可能会产生通信,既一个线程产生的结果要被另一个线程用到。

    比如常用的图片的加载就是这个样子。图片的加载是在子线程进行的,当图片加载完毕,就会回到主线程中刷新UI,展示图片。

    #import "ViewController.h"
    
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UIImageView *iconView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    }
    
    -(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event
    {
        [self performSelectorInBackground:@selector(download) withObject:nil];
    }
    -(void)download
    {
        //1.根据URL下载图片
        //从网络中下载图片
        NSURL *urlStr = [NSURL URLWithString:@"asdf"];
         //把图片转换为二进制的数据
        NSData *data = [NSData dataWithContentsOfURL:urlStr];
        //把数据转换成图片
        UIImage *image = [UIImage imageWithData:data];
        //2.回到主线程中设置图片
    
        [self performSelectorOnMainThread:@selector(settingImage:) withObject:image waitUntilDone:NO];

        //第二种方式
    
    
          //  [self.imageView performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO];      

      //第三种方式 :图片调用set方法,很好!!!

        // [self.iconView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
    } 


    //设置显示图片 -(void)settingImage:(UIImage *)image { self.iconView.image=image; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; }
    @end

    本文参考文顶顶的博客:

    http://www.cnblogs.com/wendingding/p/3805884.html

  • 相关阅读:
    用Fusion Log诊断同一版本冲突问题解决
    SQLSERVER 切换数据库为单用户和多用户模式
    redis常用命令
    linq函数All,Any,Aggregate说明
    rabbitmq部署安装
    Centos7防火墙常用命令
    SQL SERVER添加表注释、字段注释
    Windows定时任务管理以及服务管理
    SQLServer 2008数据库查看死锁、堵塞的SQL语句
    SQLServer查询死锁
  • 原文地址:https://www.cnblogs.com/huadeng/p/7019989.html
Copyright © 2011-2022 走看看