Home 合并UIImage
Post
Cancel

合并UIImage

如何合并两个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];

效果如下:

有时间实现一下微信群多头像吧。。

This post is licensed under CC BY 4.0 by the author.

函数式编程

响应式编程