2015-1-31

<pre name="code" class="objc">#import "HMViewController.h"

#define global_queue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define main_queue dispatch_get_main_queue()

@interface HMViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView1;
@property (weak, nonatomic) IBOutlet UIImageView *imageView2;
@property (weak, nonatomic) IBOutlet UIImageView *bigImageView;
@end

@implementation HMViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 创建一个组
    dispatch_group_t group = dispatch_group_create();
    
    // 开启一个任务下载图片1
    __block UIImage *image1 = nil;
    dispatch_group_async(group, global_queue, ^{
        image1 = [self imageWithURL:@"http://1.jpg"];
    });
    
    // 开启一个任务下载图片2
    __block UIImage *image2 = nil;
    dispatch_group_async(group, global_queue, ^{
        image2 = [self imageWithURL:@"http://2.jpg"];
    });
    
    // 同时执行下载图片1\下载图片2操作  
    // 等group中的所有任务都执行完毕, 再回到主线程执行其他操作
    dispatch_group_notify(group, main_queue, ^{
        self.imageView1.image = image1;
        self.imageView2.image = image2;
        
        // 合并
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 100), NO, 0.0);
        [image1 drawInRect:CGRectMake(0, 0, 100, 100)];
        [image2 drawInRect:CGRectMake(100, 0, 100, 100)];
        self.bigImageView.image = UIGraphicsGetImageFromCurrentImageContext();
        // 关闭上下文
        UIGraphicsEndImageContext();
    });

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSLog(@"-------touchesBegan");
    });
}

- (void)downlaod2image
{
    dispatch_async(global_queue, ^{
        NSLog(@"下载图片---%@", [NSThread currentThread]);
        
        // 下载图片1
        UIImage *image1 = [self imageWithURL:@"http://1.jpg"];
        NSLog(@"下载完图片1---%@", [NSThread currentThread]);
        // 下载图片2
        UIImage *image2 = [self imageWithURL:@"http://2.jpg"];
        NSLog(@"下载完图片2---%@", [NSThread currentThread]);
        
        dispatch_async(main_queue, ^{
            NSLog(@"显示图片---%@", [NSThread currentThread]);
            
            self.imageView1.image = image1;
            self.imageView2.image = image2;
            
            // 合并
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 100), NO, 0.0);
            [image1 drawInRect:CGRectMake(0, 0, 100, 100)];
            [image2 drawInRect:CGRectMake(100, 0, 100, 100)];
            self.bigImageView.image = UIGraphicsGetImageFromCurrentImageContext();
            // 关闭上下文
            UIGraphicsEndImageContext();
        });
    });
}

- (UIImage *)imageWithURL:(NSString *)urlStr
{
    NSURL *url = [NSURL URLWithString:urlStr];
    NSData *data = [NSData dataWithContentsOfURL:url]; // 这行会比较耗时
    return [UIImage imageWithData:data];
}

- (void)delay
{
    // 1.全局并发队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    // 2.计算任务执行的时间
    dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC));
    
    // 3.会在when这个时间点, 执行queue中的任务
    dispatch_after(when, queue, ^{
        NSLog(@"----run----%@", [NSThread currentThread]);
    });
}

@end

#import "HMViewController.h"@interface HMViewController ()@end@implementation HMViewController- (void)viewDidLoad{ [super viewDidLoad]; [self performSelectorInBackground:@selector(test) withObject:nil]; // [self syncMainQueue];}- (void)test{ NSLog(@"test --- %@", [NSThread currentThread]); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSLog(@"任务 --- %@", [NSThread currentThread]); });}/** * 使用dispatch_async异步函数, 在主线程中往主队列中添加任务 */- (void)asyncMainQueue{ // 1.获得主队列 dispatch_queue_t queue = dispatch_get_main_queue(); // 2.添加任务到队列中 执行 dispatch_async(queue, ^{ NSLog(@"----下载图片1-----%@", [NSThread currentThread]); });}/** * 使用dispatch_sync同步函数, 在主线程中往主队列中添加任务 : 任务无法往下执行 */- (void)syncMainQueue{ // 1.获得主队列 dispatch_queue_t queue = dispatch_get_main_queue(); // 2.添加任务到队列中 执行 dispatch_sync(queue, ^{ NSLog(@"----下载图片1-----%@", [NSThread currentThread]); });// dispatch_sync(queue, ^{// NSLog(@"----下载图片2-----%@", [NSThread currentThread]);// });// dispatch_sync(queue, ^{// NSLog(@"----下载图片3-----%@", [NSThread currentThread]);// }); // 不会开启新的线程, 所有任务在主线程中执行}// 凡是函数名种带有create\copy\new\retain等字眼, 都需要在不需要使用这个数据的时候进行release// GCD的数据类型在ARC环境下不需要再做release// CF(Core Foundation)的数据类型在ARC环境下还是需要再做release/** * 用dispatch_sync同步函数往串行列中添加任务 */- (void)syncSerialQueue{ // 1.创建串行队列 dispatch_queue_t queue = dispatch_queue_create("com.itheima.queue", NULL); // 2.添加任务到队列中 执行 dispatch_sync(queue, ^{ NSLog(@"----下载图片1-----%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"----下载图片2-----%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"----下载图片3-----%@", [NSThread currentThread]); }); // 3.释放资源// dispatch_release(queue); // MRC(非ARC) // 总结: 不会开启新的线程}/** * 用dispatch_sync同步函数往并发队列中添加任务 */- (void)syncGlobalQueue{ // 1.获得全局的并发队列 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 2.添加任务到队列中 执行 dispatch_sync(queue, ^{ NSLog(@"----下载图片1-----%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"----下载图片2-----%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"----下载图片3-----%@", [NSThread currentThread]); }); // 总结: 不会开启新的线程, 并发队列失去了并发的功能}/** * 用dispatch_async异步函数往串行队列中添加任务 */- (void)asyncSerialQueue{ // 1.创建串行队列 dispatch_queue_t queue = dispatch_queue_create("com.itheima.queue", NULL); // 2.添加任务到队列中 执行 dispatch_async(queue, ^{ NSLog(@"----下载图片1-----%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"----下载图片2-----%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"----下载图片3-----%@", [NSThread currentThread]); }); // 总结: 只开1个线程执行任务}/** * 用dispatch_async异步函数往并发队列中添加任务 */- (void)asyncGlobalQueue{ // 1.获得全局的并发队列 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 2.添加任务到队列中 执行 dispatch_async(queue, ^{ NSLog(@"----下载图片1-----%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"----下载图片2-----%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"----下载图片3-----%@", [NSThread currentThread]); }); // 总结: 同时开启了3个线程}@end
 
</pre><pre name="code" class="objc">
</pre><p></p><p><pre name="code" class="objc">#import "HMViewController.h"

@interface HMViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation HMViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        NSLog(@"--download--%@", [NSThread currentThread]);
        // 下载图片
        NSURL *url = [NSURL URLWithString:@"http://1.jpg"];
        NSData *data = [NSData dataWithContentsOfURL:url]; // 这行会比较耗时
        UIImage *image = [UIImage imageWithData:data];
        
        // 回到主线程显示图片
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"--imageView--%@", [NSThread currentThread]);
            self.imageView.image = image;
        });
    });
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值