How do I create stroked path without creating copy by calling cgpathcreatecopybystrokingpath?

StackOverflow https://stackoverflow.com/questions/23449439

  •  14-07-2023
  •  | 
  •  

Domanda

As the title says i wonder if it is possible to create a path without creating a copy of it. Basically why I need to do so is because I need a path that I can use CGpathcontainspoint.

 CGPathMoveToPoint(cgpath, NULL, lastPoint.x, lastPoint.y);
    CGPathAddLineToPoint(cgpath, NULL, newPoint.x, newPoint.y);

    UIBezierPath *tmppath = [UIBezierPath bezierPathWithCGPath:cgpath];
    tmppath.lineWidth = 6;
    [[UIColor blackColor] setStroke];
    [tmppath stroke];

    self.containspoint =  [tmppath containsPoint:imaginarynewpos1];

This get me the error message: "CGContextAddPath: invalid context 0x0. This is a serious error" Is there something I am missing?

Thanks.

È stato utile?

Soluzione

You can't. CGPathCreateCopyByStrokingPath() is the way to create a path that represents what would be drawn if some other path were stroked (i.e. drawn) with the relevant drawing parameters. Why are you searching for a different way?

There's a need to create a new path because that new path is a different path from the original. A path is conceptually a geometric object consisting of "mathematical" lines and curves. Those lines and curves have no width. Line width is conceptually a drawing parameter, not a property of the path. (This is confusing because both UIBezierPath and NSBezierPath have drawing related properties. Those are just a convenience. The CGContext API makes the separation clearer. It would be more consistent but less convenient if line width were set on the current context, like the color is.)

The cgpath in your code snippet is a line. The path created by stroking that, assuming either butt or square line cap, would be a rectangle. Those are two very different paths. You have to create the second one to test if a point is within it. Even if there were an API to do what you want directly, the framework would still have to compute the second path behind the scenes to answer the question.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top