How to use CGPathApply properly

2020-06-03 05:56发布

I'm trying to use CGPathApply to iterate over each CGPathElement in a CGPathRef object (mainly to write a custom way to persist CGPath data). The problem is, each time it get to the call to CGPathApply, my program crashes without any information at all. I suspect the problem is in the applier function, but I can't tell. Here is a sample of my code:

- (IBAction) processPath:(id)sender {
 NSMutableArray *pathElements = [NSMutableArray arrayWithCapacity:1];
    // This contains an array of paths, drawn to this current view
 CFMutableArrayRef existingPaths = displayingView.pathArray;
 CFIndex pathCount = CFArrayGetCount(existingPaths);
 for( int i=0; i < pathCount; i++ ) {
  CGMutablePathRef pRef = (CGMutablePathRef) CFArrayGetValueAtIndex(existingPaths, i);
  CGPathApply(pRef, pathElements, processPathElement);
 }
}

void processPathElement(void* info, const CGPathElement* element) {
 NSLog(@"Type: %@ || Point: %@", element->type, element->points);
}

Any ideas as to why the call to this applier method seems to be crashing? Any help is greatly appreciated.

1条回答
够拽才男人
2楼-- · 2020-06-03 06:37

element->points is a C array of CGPoint's, you can't print it out with that format specifier.

The trouble is, there's no way to tell how many elements that array holds (none that I can think of anyway). So you'll have to guess based on the type of operation, but most of them take a single point as an argument (CGPathAddLineToPoint, for example).

So a proper way to print it out would be

CGPoint pointArg = element->points[0];
NSLog(@"Type: %@ || Point: %@", element->type, NSStringFromCGPoint(pointArg));

for a path operation that takes a single point as an argument.

Hope that helps!

查看更多
登录 后发表回答