美文网首页
GCD的线程组

GCD的线程组

作者: 小苗晓雪 | 来源:发表于2017-07-12 14:48 被阅读10次
    效果图 Gif.gif

    ViewController.m

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @property (weak, nonatomic) IBOutlet UIImageView *leftImageView;
    @property (weak, nonatomic) IBOutlet UIImageView *rightImageView;
    
    @end
    
    @implementation ViewController
    
    #pragma mark - 生命周期
    - (void)viewDidLoad {
        [super viewDidLoad];
        
    }
    
    
    #pragma mark - 下载图片
    - (IBAction)downloadImage:(id)sender {
        
        self.leftImageView.image = nil;
        self.rightImageView.image = nil;
        
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSURL *url = [NSURL URLWithString:@"https://pixabay.com/static/uploads/photo/2014/09/23/21/23/iphone-6-458159_960_720.jpg"];
            NSData *data = [NSData dataWithContentsOfURL:url];
            UIImage *image = [UIImage imageWithData:data];
            dispatch_async(dispatch_get_main_queue(), ^{
                self.leftImageView.image = image;
            });
        });
        
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSURL *url = [NSURL URLWithString:@"https://pixabay.com/static/uploads/photo/2014/09/23/21/23/iphone-6-458159_960_720.jpg"];
            NSData *data = [NSData dataWithContentsOfURL:url];
            UIImage *image = [UIImage imageWithData:data];
            dispatch_async(dispatch_get_main_queue(), ^{
                self.rightImageView.image = image;
            });
        });
        
    }
    
    
    #pragma mark - 成组下载图片
    - (IBAction)groupDownloadImage:(id)sender {
        
        self.leftImageView.image = nil;
        self.rightImageView.image = nil;
        
    //    __block UIImage *rightImage = nil;
    //    __block UIImage *leftImage = nil;
    
        __block UIImage *leftImage , *rightImage = nil;
    
        
        dispatch_group_t group = dispatch_group_create();
        
        dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
            NSURL *url = [NSURL URLWithString:@"https://pixabay.com/static/uploads/photo/2014/09/23/21/23/iphone-6-458159_960_720.jpg"];
            NSData *data = [NSData dataWithContentsOfURL:url];
            leftImage = [UIImage imageWithData:data];
            //具体的赋值操作要放到后面去做;
            
        });
        
        dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
            NSURL *url = [NSURL URLWithString:@"https://pixabay.com/static/uploads/photo/2014/09/23/21/23/iphone-6-458159_960_720.jpg"];
            NSData *data = [NSData dataWithContentsOfURL:url];
            rightImage = [UIImage imageWithData:data];
            //具体的赋值操作要放到后面去做;
        });
        
        //拿到线程组里下载好的图片 , 然后回到主线程完成赋值操作:
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            //在这边永远等待 , 指导图片下载完毕我再去做其他的事情:
            dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                self.leftImageView.image = leftImage;
                self.rightImageView.image = rightImage;
            });
            
        });
        
    }
    
    
    #pragma mark - 内存警告
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        
    }
    
    
    
    @end
    
    

    相关文章

      网友评论

          本文标题:GCD的线程组

          本文链接:https://www.haomeiwen.com/subject/izsbhxtx.html