0

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).Skitch.jpg

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(UIGraphicsGetCurrentContext());
    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;
    }

}
Jordan
  • 21,746
  • 10
  • 51
  • 63

2 Answers2

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.

Anomie
  • 92,546
  • 13
  • 126
  • 145
  • Went away from this for a while. I know you probably have the right answer, I just need a little more detail. – Jordan Jan 21 '12 at 12:16
0

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.
Jordan
  • 21,746
  • 10
  • 51
  • 63