Search code examples
iphonecalayerdrawrectcgpath

create cgpath out of drawRect


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);

}

Solution

  • 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.