码迷,mamicode.com
首页 > 编程语言 > 详细

GCD线程间的通信

时间:2015-10-30 20:39:29      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:

#define HMGlobalQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define HMMainQueue dispatch_get_main_queue()

#import "HMViewController.h"

@interface HMViewController ()
@property (weak, nonatomic) IBOutlet UIButton *button;

@property (weak, nonatomic) IBOutlet UIImageView *ImageView;

@end

@implementation HMViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //[self ImageView];
    
    
//    NSObject *obj = [NSObject new];
//    
//    [[NSObject alloc] init];
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    dispatch_async(HMGlobalQueue, ^{
        NSLog(@"donwload---%@", [NSThread currentThread]);
        // 1.子线程下载图片
        NSURL *url = [NSURL URLWithString:@"http://d.hiphotos.baidu.com/image/pic/item/37d3d539b6003af3290eaf5d362ac65c1038b652.jpg"];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];
        
        // 2.回到主线程设置图片
        dispatch_async(HMMainQueue, ^{
            NSLog(@"setting---%@ %@", [NSThread currentThread], image);
            [self.button setImage:image forState:UIControlStateNormal];
        });
    });
}

@end

 

GCD线程间的通信

标签:

原文地址:http://www.cnblogs.com/ZMiOS/p/4924228.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!