iOS 多线程编程<四、GCD线程间通信>

来源:互联网 发布:专业java培训中心 编辑:程序博客网 时间:2024/05/16 18:31

GCD可以通过GCD嵌套进行数据通信

////  ViewController.m//  GCD////  Created by fe on 16/9/30.//  Copyright © 2016年 fe. All rights reserved.//#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UIImageView *imageView;@end@implementation ViewController- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{            dispatch_async(dispatch_get_global_queue(0, 0), ^{        NSURL *url = [NSURL URLWithString:@"http://p3.so.qhmsg.com/t0102d947858e015123.jpg"];        NSData *imageData = [NSData dataWithContentsOfURL:url];        UIImage *image = [UIImage imageWithData:imageData];        dispatch_async(dispatch_get_main_queue(), ^{            self.imageView.image = image;        });    });}@end

运行效果如下图

0 0
原创粉丝点击