#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView2;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesBegan");
// 在子线程下载图片
//异步函数+全局并发队列 嵌套异步函数+主队列
//异步函数+全局并发队列
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//
UIImage *image = [self downloadWebImageWithUrlString:@"http://img0.pclady.com.cn/pclady/1508/18/1346699_31.jpg"];
//异步函数+主队列
dispatch_async(dispatch_get_main_queue(), ^{
//返回要下载的图片
self.imageView.image = image;
});
});
// 在子线程下载图片
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//
UIImage *image = [self downloadWebImageWithUrlString:@"http://finance.gucheng.com/UploadFiles_7830/201507/2015071710300340.jpg"];
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView2.image = image;
});
});
}
//
- (void)grouptest
{
// 下载两张图片.等到两张图片都下载完毕之后,合并图片.然后再显示合并之后的图片.
// 创建一个队列组
dispatch_group_t group = dispatch_group_create();
__block UIImage *image1, *image2;
// 在子线程下载,并且放到一个队列组中.
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
// 下载第一张图片
image1 = [self downloadWebImageWithUrlString:@"http://img0.pclady.com.cn/pclady/1508/18/1346699_31.jpg"];
});
// 在子线程下载,并且放到一个队列组中.
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
image2 = [self downloadWebImageWithUrlString:@"http://finance.gucheng.com/UploadFiles_7830/201507/2015071710300340.jpg"];
});
// 队列组中的任务执行完毕之后的后续操作.
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// 合并图片
UIImage *image = [self bingImageWithImage1:image1 Image2:image2];
// 显示图片
self.imageView.image = image;
});
}
// 合并图片
- (UIImage *)bingImageWithImage1:(UIImage *)image1 Image2:(UIImage *)image2
{
if (!image1 || !image2) {
return nil;
}
// 开启一个图形上下文
UIGraphicsBeginImageContext(self.imageView.bounds.size);
// 绘制第一张图片
[image1 drawInRect:self.imageView.bounds];
// 绘制第二张图片
[image2 drawInRect:CGRectMake(0, self.imageView.bounds.size.height - 80, self.imageView.bounds.size.width, 80)];
// 获取合并后的图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// 关闭图形上下文
UIGraphicsEndImageContext();
return image;
}
// 下载网络图片的方法
- (UIImage *)downloadWebImageWithUrlString:(NSString *)urlString
{
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
return image;
}
- (void)test
{
// 队列组. 队列组可以等待组内的任务都执行完毕之后,再执行后续的操作.
// 从右往左解读:先将任务添加到队列中,然后在将队列添加到队列组中.
// 1.创建一个队列组
dispatch_group_t group = dispatch_group_create();
// 往队列组中添加一个任务
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
//
NSLog(@"renwu1%@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:3];
});
// 往队列组中添加一个任务
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
//
NSLog(@"renwu2%@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:4];
});
// 往队列组中添加一个任务
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
//
NSLog(@"renwu3%@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:5];
});
// 当队列组中的任务都执行完毕之后,会发出一个通知,执行下面的方法.
// 参数1: 确定哪一个队列组中的任务执行完毕.
// 参数2: 队列组中的任务执行完毕之后的后续操作需要早那条线程中执行(直接传一个队列)
// 参数3: 需要后续执行的任务
dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
NSLog(@"zuihouzhixingde renwu %@",[NSThread currentThread]);
});
}
@end