对于程序猿来说,代码比空白文字来的有力多。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"看看当前线程:%@",[NSThread currentThread]);
//[self Test1];
//[self Test2];
//[self Test3];
[self Test4];
}
-(void)Test1
{
//创建一个串行队列
dispatch_queue_t queue=dispatch_queue_create("gujinyue", DISPATCH_QUEUE_PRIORITY_DEFAULT);
// for(int i=0;i<10;i++)
// {
// dispatch_async(queue, ^{
//
// NSLog(@"开启咯一个异步任务,%@ %d",[NSThread currentThread],i);
// });
// }
// for(int i=0;i<10;i++)
// {
// dispatch_sync(queue, ^{
//
// NSLog(@"开启咯一个同步任务,%@ %d",[NSThread currentThread],i);
// });
// }
//
// for(int i=0;i<10;i++)
// {
// dispatch_async(queue, ^{
//
// NSLog(@"开启咯一个异步任务,%@ %d",[NSThread currentThread],i);
// });
// }
dispatch_sync(queue, ^{
NSLog(@"gujinyue %@",[NSThread currentThread]);
dispatch_sync(queue, ^{
NSLog(@"肯定阻塞了");
});
// dispatch_async(queue, ^{
// NSLog(@"bu阻塞了 %@",[NSThread currentThread]);
// });
});
}
-(void)Test2
{
dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSURL *url=[NSURL URLWithString:@"http://www.nbdpc.gov.cn/picture/0/s20160217090219.jpg"];
UIImage *image=[UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
NSLog(@"走在哪里?%@",[NSThread currentThread]);
//回到主线程显示图片
dispatch_async(dispatch_get_main_queue(),^{
NSLog(@"回到主线程接受图片 %@",[NSThread currentThread]);
self.imageView.image=image;
});
});
}
//这个方法是按顺序下载两张图片的,但其实下载过程并不需要按顺序执行,并发执行可以提高执行速度。
//但是注意一点就是:必须等两张图片下载完毕后才能回到主线程显示图片,所以可以考虑Dispatch Group
-(void)Test3
{
//异步下载图片
dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
//下载第一张图片
NSString *url1=@"http://www.nbdpc.gov.cn/picture/0/s20160217090219.jpg";
UIImage *image1=[self imageWithURLString:url1];
NSString *url2=@"http://www.nbdpc.gov.cn/picture/0/s20160225090242.jpg";
UIImage *image2=[self imageWithURLString:url2];
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image=image1;
self.imageView2.image=image2;
});
});
}
//根据url获取UIImage
-(UIImage *)imageWithURLString:(NSString *)urlString
{
NSURL *url=[NSURL URLWithString:urlString];
NSData *data=[NSData dataWithContentsOfURL:url];
return [UIImage imageWithData:data];
}
//***************************************************************************************
-(void)Test4
{
dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//异步下载图片
dispatch_async(queue, ^{
//创建一个组
dispatch_group_t group=dispatch_group_create();
__block UIImage *image1=nil;
__block UIImage *image2=nil;
//1.关联一个任务到group
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//下载第一张图片
NSString *url1=@"http://www.nbdpc.gov.cn/picture/0/s20160217090219.jpg";
image1=[self imageWithUrlString2:url1];
});
//2.关联一个任务到group
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//下载第二张图片
NSString *url2=@"http://www.nbdpc.gov.cn/picture/0/s20160225090242.jpg";
image2=[self imageWithUrlString2:url2];
});
//3.等待组中的任务执行完毕,回到主线程执行block回调
//dispatch_group_notify函数涌来指定一个额外的Block,该block将在group中的所有任务完成后执行
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
self.imageView.image=image1;
self.imageView2.image=image2;
});
});
}
-(UIImage *)imageWithUrlString2:(NSString *)urlString
{
NSURL *url=[NSURL URLWithString:urlString];
NSData *data=[NSData dataWithContentsOfURL:url];
//这里并没有自动释放UIImage对象
return [[UIImage alloc]initWithData:data];
}
@end