containsPoint: for UIBezierPath crashes

2019-06-16 19:57发布

问题:

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? :-(

回答1:

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



回答2:

It turns out the crash is a bug in the framework. This answer provides another way to determine whether a point is inside a UIBezierPath.