CWitty
CWitty

Reputation: 4526

Merge UIImage and UIBezierPath into 1 UIImage

I have a view that has a UIImage and a DrawView as subviews. The draw view responds to touches and creates a UIBezierPath for drawing on the image. When the user accepts their change I need to merge the underlying UIImage and any UIBezierPath's created into a single UIImage.

Upvotes: 0

Views: 417

Answers (1)

Sabir Ali
Sabir Ali

Reputation: 485

Make that UIBazierPath a public property of your DrawView, and keep updating it when user is drawing from within DrawView. When user hits the Accept button, use following code to simply Create a UIImageContext and draw both, the sourceImage and bezierPath over the context. Finally take whatever is drawn on the context in a 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.

Upvotes: 4

Related Questions