如何将两个UIImageViews“合并”成一个图像?喜欢Photoshop Layer合并?

时间:2012-09-26 12:49:49

标签: ios uiimageview drawing

我有一个绘图应用程序,你有一个UIImageView作为“绘图层”。你下面有另一个UIImageView,它是“图像层”,包含你正在绘制的图像。我喜欢这种分离。但是,我希望用户能够将他们在图像上制作的绘图“保存并通过电子邮件发送”为一个统一的图像。我该怎么做?

1 个答案:

答案 0 :(得分:4)

您的UIImageView个实例必须是UIView层次结构的一部分,所以您需要做的就是将包含UIView的顶部绘制到上下文中

UIGraphicsBeginImageContext(CGSizeMake(width, height));

[container.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *fimage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

或者如果这会让您连续将两个图像绘制成上下文

UIGraphicsBeginImageContext(CGSizeMake(width, height));

[image1.layer renderInContext:UIGraphicsGetCurrentContext()];
[image2.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *fimage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

从那里你可以写出你选择的数据

NSData *data = UIImagePNGRepresentation(fimage);

将该数据传递到MailComposer设置。