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
    时光见证了成长,还很无知,我想一点点幼稚转为有知!
  • 相关阅读:
    《模糊测试--强制发掘安全漏洞的利器》阅读笔记(一)
    BrickerBot
    这些写的很好的PCA文章
    决策树(挖坑待填)
    线性回归
    关于给定DNA序列,如何找到合理的切割位点使得其退火温度保持相对一致
    生成全排列
    AVL树学习笔记
    二叉搜索树
    堆排序
  • 原文地址:https://www.cnblogs.com/foreveriOS/p/5416157.html
Copyright © 2011-2022 走看看