将UIImage和UIBezierPath合并为1个UIImage

时间:2015-04-10 22:04:17

标签: uiimageview uiimage core-graphics uibezierpath

我的视图中有UIImageDrawView作为子视图。绘制视图响应触摸并创建UIBezierPath以在图像上绘制。当用户接受他们的更改时,我需要将创建的基础UIImage和任何UIBezierPath合并为一个UIImage

1 个答案:

答案 0 :(得分:4)

UIBazierPath作为DrawView的公共财产,并在用户从DrawView内抽取时继续更新。当用户点击“接受”按钮时,使用以下代码只需在上下文中创建UIImageContext绘制两者,sourceImagebezierPath。最后,在resultImage中对上下文中绘制的内容进行处理。

UIBezierPath *path = drawView.Path // Your Bezier Path in DrawView
UIImage *sourceImage = sourceImageView.image; // Your Source Image from ImageView.
UIImage *resultImage = nil;
UIGraphicsBeginImageContext(sourceImage.size);
[sourceImage drawInRect:(CGRect){CGPointZero, sourceImage.size}];
[path stroke]; //Fill or Stroke path as you need.
[path fill];

resultImage = UIGraphicsGetImageFromCurrentImageContext(); //taking the merged result from context, in a new Image. This is your required image.

UIGraphicsEndImageContext();

//Use resultImage as you want.