Creating Mask from Path
Not a CoreGraphics expert, I'd really appreciate some help.
I'm trying to create a mask from a path. (See red path in image).
Instead of the red path, I'd like the path to be a mask to an image underneath.
Could really use a hand. Thanks in advance
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y -= 20;
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 20.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCu开发者_运维技巧rrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
mouseMoved++;
if (mouseMoved == 10) {
mouseMoved = 0;
}
}
Once you have the path created, use CGContextReplacePathWithStrokedPath
convert it to a path you can fill to get the same results as stroking the path. Then you can use CGContextClip
to set the clipping mask to this path.
The solution I came up with was:
- Have UIView image on screen.
- Create UIImageView image with [UIColor clearColor] background color
- Use CoreGraphics to fill UIImageView with color
- Save UIImageView, then in TouchesMoved or UIPanGestureRecognizer, draw savedImage, Stroke Path after setting kCGContentBlendClear, save Image and repeat.
精彩评论