2

I have UIView subclass and I want to draw a CGPath out of the drawRect. I have heard that it is and isn't possible. I understand that the CGPath needs context. I tried to give it context with the newImage. I am also thinking that I have to call this differently for example my code in a CCLayer for the view to display what would be in the drawRect

- (void)createPath:(CGPoint)origin
{
CGSize size=CGSizeMake(320, 480);
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsBeginImageContextWithOptions(size,YES,1.0f);
CGContextSaveGState(context);

UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext();


CGRect newRect= CGRectMake(0, 0, 320, 480);
CGMutablePathRef path1 = CGPathCreateMutable();

CGPathMoveToPoint(path1, nil,100, 200);

CGPoint newloc = CGPointMake(300, 130);

CGPathMoveToPoint(path1, NULL, newloc.x, newloc.y);
CGPathAddLineToPoint(path1, NULL, newloc.x + 16,newloc.y + 38);
CGPathAddLineToPoint(path1, NULL, newloc.x + 49, newloc.y + 0);
CGPathAddLineToPoint(path1, NULL, newloc.x + 23,  newloc.y - 39);
CGPathAddLineToPoint(path1, NULL, newloc.x - 25,newloc.y - 40);
CGPathAddLineToPoint(path1, NULL, newloc.x -43, newloc.y + 0);
CGPathCloseSubpath(path1);

CGContextAddPath(context, path1);

CGContextSaveGState(context);
CGContextSetLineWidth(context, 10.0);
CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
//Fill and stroke
CGContextDrawPath(context, kCGPathFillStroke);
//CGContextDrawImage(context, newRect, myImage.CGImage);

UIGraphicsEndImageContext();
CGContextRestoreGState(context);
CGPathRelease(path1); 
CGContextRelease(context);

}
Asdrubal
  • 2,421
  • 4
  • 29
  • 37
  • Are you looking to take the image out of the path drawn context? – cocoakomali Mar 07 '12 at 19:03
  • I just want to make a CGPath to look like a trail. I know how to do it when I do everything in drawRect. I don't know how to draw a CGPath outside of the drawRect. I know I need context and I thought I needed to make an image to draw on. – Asdrubal Mar 07 '12 at 21:13

1 Answers1

0

See if this helps you. Have a UIView with UIImageView as it's subview. Then implement the touches method as below:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];   

    //Get touch point
    CGPoint currentPoint = [touch locationInView:drawImage];

    UIGraphicsBeginImageContext(self.frame.size);

    //drawImage is the UIImageView
    [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; 

    //Line attributes
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);

    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0);

    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.5, 1.0, 1.0);

    //Begin path
    CGContextBeginPath(UIGraphicsGetCurrentContext());

    CGPathMoveToPoint(drawPath, NULL, lastPoint.x, lastPoint.y);

    CGPathAddLineToPoint(drawPath, NULL, currentPoint.x, currentPoint.y);

    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());

    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
}

Now you have the UIImageView visible with what you drew and drawPath will have your path.

cocoakomali
  • 1,346
  • 1
  • 8
  • 7
  • How would I make the UIImageView? What is point of the drawInRect method (for knowledge sake)? – Asdrubal Mar 12 '12 at 16:29
  • I figured out the first part of my comment (above). However I am not sure the general solution. Here is what I have... There is UIView. I have a CCLayer where I add the UIView to display it. I have a createPath method. I then create a CGPath with in the method. The method contains your code. I dont have anything shown. Are you making the CGPath in the drawInRect? Secondary question. Would the method that you created be drawn in a CCLayer or ViewController and drawInRect would be a method in my UIView method? Could you explain where and why you code belongs. I am very dumb. – Asdrubal Mar 12 '12 at 16:47
  • This code belongs to a UIView class. This class in turn has a UIImageView subview. The touch methods are used to capture the path. I use the image view to display the scribbling done. – cocoakomali Mar 12 '12 at 18:04
  • Ok thanks for all your help I found what I needed to do. Some times I can't see the answer right in front of me. I forgot to add [self addSubview:drawImage]; – Asdrubal Mar 12 '12 at 18:13
  • No problem. Don't forget to tick my answer! – cocoakomali Mar 12 '12 at 18:15