マルチスレッド-GCDスレッド間通信Demo

1165 ワード

画像を非同期でダウンロードすることで、メインキュー列がUIプレゼンテーションスレッド間の通信使用をリフレッシュ
注意点:
1.          
2.  UI      

サンプルコード

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController{
    
    //    
    UIImageView *_imageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //       
    _imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:_imageView];
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    //         
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://m.818today.com/imgsy/image/2016/0113/6358830376853382456131864.jpg"]];
        UIImage *downImage = [UIImage imageWithData:data];
        
        //   UI(                ,  GCD            )
        dispatch_async(dispatch_get_main_queue(), ^{
            
            _imageView.image = downImage;
        });
        
    });
    
}

@end