#import "ViewController.h"
@interface ViewController ()
{
UIImageView *_imageView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
__block UIImage *image1;
__block UIImage *image2;
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"----下载图片1-----%@", [NSThread currentThread]);
NSURL *url1 = [NSURL URLWithString:@"http://a.hiphotos.baidu.com/image/pic/item/8718367adab44aed3eb376e9b11c8701a18bfb3a.jpg"];
NSData *data1 = [NSData dataWithContentsOfURL:url1];
//NSLog(@"data1 %@",data1);
image1 = [UIImage imageWithData:data1];
}];
[operation addExecutionBlock:^{
NSLog(@"-----下载图片2----%@",[NSThread currentThread]);
NSURL *url2 = [NSURL URLWithString:@"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superplus/img/logo_white_ee663702.png"];
NSData *data2 = [NSData dataWithContentsOfURL:url2];
//NSLog(@"data2 %@", data2);
image2 = [UIImage imageWithData:data2];
}];
[operation setCompletionBlock:^{
NSLog(@"-----合并图片------%@",[NSThread currentThread]);
if (image1 && image2) {
//开启一个位图上下文
UIGraphicsBeginImageContext(image1.size);
//绘制图片1
[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
//绘制图片2
[image2 drawInRect:CGRectMake(image1.size.width-image2.size.width*0.4, image1.size.height-image2.size.height*0.4, image2.size.width*0.4, image2.size.height*0.4)];
NSString *name=@"范冰冰";
//属性字典
NSDictionary *attributeDic=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],NSForegroundColorAttributeName,[UIFont boldSystemFontOfSize:50],NSFontAttributeName, nil];
//画NSString
[name drawAtPoint:CGPointMake(0, image1.size.height-50-5) withAttributes:attributeDic];
//画label
UILabel *description=[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 50)];
description.textColor=[UIColor purpleColor];
[description setFont:[UIFont systemFontOfSize:50]];
[description setText:@"还珠格格"];
//获取label图片
UIImage *labelImage=[self getImageFromLabel:description];
[labelImage drawInRect:CGRectMake(0, 80, labelImage.size.width, labelImage.size.height)];
//得到上下文中的图片
UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
//结束上下文
UIGraphicsEndImageContext();
// //1.performSelectorOnMainThread回到主线程
// [self performSelectorOnMainThread:@selector(setImage:) withObject:fullImage waitUntilDone:NO];
// //2.GCD回到主线程
// dispatch_async(dispatch_get_main_queue(), ^{
// _imageView = [[UIImageView alloc] init];
// [_imageView setFrame:CGRectMake(0, 0, fullImage.size.width*0.4, fullImage.size.height*0.4)];
// [self.view addSubview:_imageView];
// [_imageView setImage:fullImage];
// });
//3.[NSOperationQueue mainQueue]回到主线程
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[mainQueue addOperationWithBlock:^{
_imageView = [[UIImageView alloc] init];
[_imageView setFrame:CGRectMake(0, 0, fullImage.size.width*0.4, fullImage.size.height*0.4)];
[self.view addSubview:_imageView];
[_imageView setImage:fullImage];
}];
}
}];
[queue addOperation:operation];
}
//把label转换成图片
-(UIImage *)getImageFromLabel:(UILabel *)label
{
//开启上下文
UIGraphicsBeginImageContext(label.frame.size);
//获取当前上下文
CGContextRef currentContent=UIGraphicsGetCurrentContext();
[label.layer drawInContext:currentContent];
//获得当前上下文中绘制的图片
UIImage *labelImage=UIGraphicsGetImageFromCurrentImageContext();
//结束上下文
UIGraphicsEndImageContext();
return labelImage;
}
- (void)setImage:(UIImage *)image
{
NSLog(@"setImage ---%@",[NSThread currentThread]);
_imageView = [[UIImageView alloc] init];
[_imageView setFrame:CGRectMake(0, 0, image.size.width*0.4, image.size.height*0.4)];
[self.view addSubview:_imageView];
[_imageView setImage:image];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end