如何合并两个UIImage生产一个UIImage?
1、准备两个UIImage
1
2
UIImage * image1 = [UIImage imageNamed:@"1.jpg"];
UIImage * image2 = [UIImage imageNamed:@"2.jpg"];
2、指定合并后UIImage的大小
1
CGSize size = CGSizeMake(400, 200);
3、创建一个基于位图的上下文UIGraphicsBeginImageContext(size),并将其设置为当前上下文(context)
1
2
//上下文
UIGraphicsBeginImageContext(size);
4、绘制图形,给定每个图片绘制的frame
1
2
[image1 drawInRect:CGRectMake(0, 0, 100,200)];
[image2 drawInRect:CGRectMake(100, 0, 100, 200)];
5、通过此上下文生产新的UIImage
1
2
3
4
UIImage * togetherImage = UIGraphicsGetImageFromCurrentImageContext();
//结束上下文
UIGraphicsEndImageContext();
6、此时可以将图像展示出来啦
1
2
3
UIImageView * imgV = [[UIImageView alloc]initWithImage:togetherImage];
imgV.center = CGPointMake(SCREEN_SIZE.width/2.0, SCREEN_SIZE.height/2.0);
[self.view addSubview:imgV];