zoukankan      html  css  js  c++  java
  • 线程间通信-03-GCD

    //
    //  ViewController.m
    //  03-GCD-线程间通信
    //
    //  Created by mac on 16/4/21.
    //  Copyright © 2016年 mac. All rights reserved.
    //
    #define GlobalQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
    #define MainQueue dispatch_get_main_queue()
    
    
    
    #import "ViewController.h"
    
    @interface ViewController ()
    /**
     *   oc语法中:1. 属性名不能以new开头  2. init开头的构造方法中才允许self进行赋值
     */
    //@property (weak, nonatomic) IBOutlet UIImageView *newImageView;
    
    @property (weak, nonatomic) IBOutlet UIButton *buttonImage;
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
    }
    /**
     *  属性不能以new开头,new有特殊含义
     */
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        
        dispatch_async(GlobalQueue, ^{
           
            //1. 子线程中下载图片
            NSURL *url = [NSURL URLWithString:@"http://img0.imgtn.bdimg.com/it/u=4168762024,1922499492&fm=21&gp=0.jpg"];
            NSData *data = [NSData dataWithContentsOfURL:url];
            UIImage *image = [UIImage imageWithData:data];
            
            NSLog(@"===%@,%@", image, [NSThread currentThread]);
            
            //2. 主线程中设置图片
            dispatch_async(MainQueue, ^{
               
                [self.buttonImage setBackgroundImage:image forState:UIControlStateNormal];
                NSLog(@"=%@,%@", image, [NSThread currentThread]);
    
            });
        });
    }
    
    @end
    时光见证了成长,还很无知,我想一点点幼稚转为有知!
  • 相关阅读:
    linux tar.gz zip 解压缩 压缩命令
    VC中获取窗体句柄的各种方法
    CodeForces 377B---Preparing for the Contest(二分+贪心)
    Response.Write具体介绍
    Linux实现字符设备驱动的基础步骤
    Android Bundle类
    Otacle表查询
    android该系统的应用API选择演示版本
    微机原理(2)8086
    大话设计の创建模式
  • 原文地址:https://www.cnblogs.com/foreveriOS/p/5416157.html
Copyright © 2011-2022 走看看