Turning a freehand drawing in a UIImageView into a mask

361 views Asked by At

I am making an app that let's the user draw over an object/person in a image which I then want to act as a mask in order to blur the background of the image.

I am using the following code to allow the user to draw on an image view that is above the image view with the image to be manipulated:

mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];

UIGraphicsBeginImageContext(self.view.frame.size);
[self.drawImage.image drawInRect:CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeNormal);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), firstPoint.x, firstPoint.y);
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [UIColor blackColor].CGColor);
CGContextFillPath(UIGraphicsGetCurrentContext());
CGContextStrokePath(UIGraphicsGetCurrentContext());


self.drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.drawImage setAlpha:opacity];

This works fine and achieves the result I want, and I can then save this drawing image as I wish from within the app.

However, I want to be able to make the drawing into a mask so that I can then apply it to the original image and a blurred image in order to make the object under the mask appear in focus.

Is there a way that I can turn the drawing into a mask?

0

There are 0 answers