方法一
先下載第1張圖片涩僻,然后下載第2張圖片晌涕,最后合并圖片
弊病:由于圖片比較耗時(shí)撞鹉,一張一張的下載圖片更耗時(shí)疟丙。
// 異步下載
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 1.下載第1張
NSURL *url1 = [NSURL URLWithString:@"http://g.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494ee460de6182ff5e0fe99257e80.jpg"];
NSData *data1 = [NSData dataWithContentsOfURL:url1];
UIImage *image1 = [UIImage imageWithData:data1];
// 2.下載第2張
NSURL *url2 = [NSURL URLWithString:@"http://su.bdimg.com/static/superplus/img/logo_white_ee663702.png"];
NSData *data2 = [NSData dataWithContentsOfURL:url2];
UIImage *image2 = [UIImage imageWithData:data2];
// 3.合并圖片
// 開(kāi)啟一個(gè)位圖上下文
UIGraphicsBeginImageContextWithOptions(image1.size, NO, 0.0);
// 繪制第1張圖片
CGFloat image1W = image1.size.width;
CGFloat image1H = image1.size.height;
[image1 drawInRect:CGRectMake(0, 0, image1W, image1H)];
// 繪制第2張圖片
CGFloat image2W = image2.size.width * 0.5;
CGFloat image2H = image2.size.height * 0.5;
CGFloat image2Y = image1H - image2H;
[image2 drawInRect:CGRectMake(0, image2Y, image2W, image2H)];
// 得到上下文中的圖片
UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
// 結(jié)束上下文
UIGraphicsEndImageContext();
// 4.回到主線(xiàn)程顯示圖片
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = fullImage;
});
});
方法二
同時(shí)開(kāi)啟兩個(gè)線(xiàn)程分別下載圖片
弊病:增加額外的屬性
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 異步下載
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 1.下載第1張
NSURL *url1 = [NSURL URLWithString:@"http://g.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494ee460de6182ff5e0fe99257e80.jpg"];
NSData *data1 = [NSData dataWithContentsOfURL:url1];
self.image1 = [UIImage imageWithData:data1];
[self bindImages];
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 2.下載第2張
NSURL *url2 = [NSURL URLWithString:@"http://su.bdimg.com/static/superplus/img/logo_white_ee663702.png"];
NSData *data2 = [NSData dataWithContentsOfURL:url2];
self.image2 = [UIImage imageWithData:data2];
[self bindImages];
});
}
- (void)bindImages
{
if (self.image1 == nil || self.image2 == nil) return;
// 3.合并圖片
// 開(kāi)啟一個(gè)位圖上下文 (相當(dāng)于創(chuàng)建了一個(gè)空的圖片)
// UIGraphicsBeginImageContextWithOptions(<#CGSize size#>(新的圖片有多大), <#BOOL opaque#>(NO代表透明鸟雏,YES代表不透明), <#CGFloat scale#> (縮放比例))
UIGraphicsBeginImageContextWithOptions(self.image1.size, NO, 0.0);
// 繪制第1張圖片 (繪制到上面創(chuàng)建的那個(gè)位圖上下文上面 也就是那張空的圖片上面)
CGFloat image1W = self.image1.size.width;
CGFloat image1H = self.image1.size.height;
[self.image1 drawInRect:CGRectMake(0, 0, image1W, image1H)];
// 繪制第2張圖片
CGFloat image2W = self.image2.size.width * 0.5;
CGFloat image2H = self.image2.size.height * 0.5;
CGFloat image2Y = image1H - image2H;
//把image2 繪制到后面的那個(gè)區(qū)域
[self.image2 drawInRect:CGRectMake(0, image2Y, image2W, image2H)];
// 得到上下文中的圖片
UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
// 結(jié)束上下文
UIGraphicsEndImageContext();
// 4.回到主線(xiàn)程顯示圖片
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = fullImage;
});
}
方法三
隊(duì)列組
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.隊(duì)列組
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 2.下載圖片1
__block UIImage *image1 = nil; //要加一個(gè) __block因?yàn)?block代碼默認(rèn)不能改外面的東西(記住語(yǔ)法即可)
dispatch_group_async(group, queue, ^{
NSURL *url1 = [NSURL URLWithString:@"http://g.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494ee460de6182ff5e0fe99257e80.jpg"];
NSData *data1 = [NSData dataWithContentsOfURL:url1];
image1 = [UIImage imageWithData:data1];
});
// 3.下載圖片2
__block UIImage *image2 = nil;
dispatch_group_async(group, queue, ^{
NSURL *url2 = [NSURL URLWithString:@"http://su.bdimg.com/static/superplus/img/logo_white_ee663702.png"];
NSData *data2 = [NSData dataWithContentsOfURL:url2];
image2 = [UIImage imageWithData:data2];
});
// 4.合并圖片 用Quartz2D的知識(shí)享郊,則要先要搞一個(gè)空的大圖片,然后再把小圖片畫(huà)上去(保證執(zhí)行完組里面的所有任務(wù)之后孝鹊,再執(zhí)行notify函數(shù)里面的block)
//隊(duì)列組:要把隊(duì)列組里面的所有任務(wù)都執(zhí)行完后調(diào)用dispatch_group_notify(group, queue, ^{ }
dispatch_group_notify(group, queue, ^{
// 開(kāi)啟一個(gè)位圖上下文
UIGraphicsBeginImageContextWithOptions(image1.size, NO, 0.0);
// 繪制第1張圖片
CGFloat image1W = image1.size.width;
CGFloat image1H = image1.size.height;
[image1 drawInRect:CGRectMake(0, 0, image1W, image1H)];
// 繪制第2張圖片
CGFloat image2W = image2.size.width * 0.5;
CGFloat image2H = image2.size.height * 0.5;
CGFloat image2Y = image1H - image2H;
[image2 drawInRect:CGRectMake(0, image2Y, image2W, image2H)];
// 得到上下文中的圖片
UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
// 結(jié)束上下文
UIGraphicsEndImageContext();
// 5.回到主線(xiàn)程顯示圖片
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = fullImage;
});
});
}
參考 ** iOS開(kāi)發(fā)多線(xiàn)程之隊(duì)列組——下載合并圖片**
方法四
線(xiàn)程依賴(lài)
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
/**
假設(shè)有A炊琉、B、C三個(gè)操作又活,要求:
1. 3個(gè)操作都異步執(zhí)行
2. 操作C依賴(lài)于操作A
3. 操作C依賴(lài)于操作B
*/
//創(chuàng)建一個(gè)隊(duì)列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//可開(kāi)辟線(xiàn)程的最大數(shù)量
queue.maxConcurrentOperationCount = 3;
//創(chuàng)建三個(gè)任務(wù)
NSBlockOperation *operationA = [NSBlockOperation blockOperationWithBlock:^{
NSURL *url1 = [NSURL URLWithString:@"http://g.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494ee460de6182ff5e0fe99257e80.jpg"];
NSData *data1 = [NSData dataWithContentsOfURL:url1];
self.image1 = [UIImage imageWithData:data1];
}];
NSBlockOperation *operationB = [NSBlockOperation blockOperationWithBlock:^{
NSURL *url2 = [NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1504108008401&di=6297b96e3a13728c58d806b71e4590bb&imgtype=0&src=http%3A%2F%2Fwww.logoing.net%2Fwp-content%2Fuploads%2F2012%2F08%2Fbaidulogo1.jpg"];
NSData *data2 = [NSData dataWithContentsOfURL:url2];
self.image2 = [UIImage imageWithData:data2];
}];
NSBlockOperation *operationC = [NSBlockOperation blockOperationWithBlock:^{
// 開(kāi)啟一個(gè)位圖上下文
UIGraphicsBeginImageContextWithOptions(self.image1.size, NO, 0.0);
// 繪制第1張圖片
CGFloat image1W = self.image1.size.width;
CGFloat image1H = self.image1.size.height;
[self.image1 drawInRect:CGRectMake(0, 0, image1W, image1H)];
// 繪制第2張圖片
CGFloat image2W = self.image2.size.width * 0.5;
CGFloat image2H = self.image2.size.height * 0.5;
CGFloat image2Y = image1H - image2H;
[self.image2 drawInRect:CGRectMake(0, image2Y, image2W, image2H)];
// 得到上下文中的圖片
UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
// 結(jié)束上下文
UIGraphicsEndImageContext();
// 5.回到主線(xiàn)程顯示圖片
dispatch_async(dispatch_get_main_queue(), ^{
self.ww_imgView.image = fullImage;
});
}];
//設(shè)置任務(wù)之間的依賴(lài)
[operationC addDependency:operationA];
[operationC addDependency:operationB];
//添加操作到隊(duì)列中-自動(dòng)異步執(zhí)行苔咪,并發(fā)
[queue addOperation:operationA];
[queue addOperation:operationB];
[queue addOperation:operationC];
}
參考iOS-網(wǎng)絡(luò)圖片通過(guò)NSOperation線(xiàn)程依賴(lài)異步加載并保存到沙盒中