码迷,mamicode.com
首页 > 移动开发 > 详细

IOS第二天多线程-04GCD通信

时间:2015-09-08 19:46:00      阅读:155      评论: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 *newImageView;
@end

@implementation HMViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self newImageView];
    
    
//    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

 

IOS第二天多线程-04GCD通信

标签:

原文地址:http://www.cnblogs.com/ios-g/p/4792498.html

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