Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

containsPoint: for UIBezierPath crashes

I'm parsing a SVG file to UIBezierPath. I'd like to know whether a CGPoint is inside or outside the UIBezierPath. To do this I use containsPoint:. When I run this in the simulator everthing works fine (almost!). When I run it a iPad device it crashes on some paths and points. I'm calling closePath on every UIBezierPath before I call containsPoint:.

Here are some paths and points, which makes it crash (I randomize the points, so there might be more crash points):

Crash Points: (659.0, 444.0), (659.0, 443.0)
SVG Path: M661 446 c1 -1 3 -1 4 -1 1 -1 2 -2 2 -4 0 -2 0 -2 -2 -2 0 1 -2 1 -3 1 -2 0 -3 1 -3 2 0 1 0 2 0 3 0 0 1 1 2 1z

Crash Points: (533.0, 458.0), (533.0, 457.0)
SVG Path: M535 460 c0 0 1 -1 1 -2 1 -2 0 -3 -1 -3 0 0 -1 0 -2 1 0 1 0 2 0 3 1 0 1 1 2 1z

I can't figure out why it crashes. To me it should return YES or NO no matter what path or point it gets.

Why does it crash? :-(

like image 627
ThomasCle Avatar asked Sep 19 '12 07:09

ThomasCle


People also ask

What is a UIBezierPath object?

A UIBezierPath object combines the geometry of a path with attributes that describe the path during rendering. You set the geometry and attributes separately and can change them independent of one another. After you have the object configured the way you want it, you can tell it to draw itself in the current context.

How do I Close a subpath in béZier?

A single Bézier path object can contain any number of open or closed subpaths, where each subpath represents a connected series of path segments. Calling the close () method closes a subpath by adding a straight line segment from the current point to the first point in the subpath.

Can I use béZier path objects multiple times?

Because the creation, configuration, and rendering process are all distinct steps, Bézier path objects can be reused easily in your code. You can even use the same object to render the same shape multiple times, perhaps changing the rendering options between successive drawing calls.


1 Answers

Try the hit testing techniques in this post:

http://oleb.net/blog/2012/02/cgpath-hit-testing/

I've use this in one of my apps and it works without any problems. His examples were all I needed to get things working. If you need more help I can post some of my code tonight.

If you need any help with the SVG parsing, I forked a github project to do parsing of SVG paths, and extended it to support line and polyline elements. I'm converting SVGs to UIBezierPaths and then doing animation and hit testing for finger drawing.

https://github.com/stevekohls/PocketSVG

like image 168
stevekohls Avatar answered Sep 20 '22 20:09

stevekohls