Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use CGPathApply properly

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.

like image 620
Eric Avatar asked Sep 15 '10 18:09

Eric


1 Answers

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!

like image 75
SuperNES Avatar answered Nov 07 '22 12:11

SuperNES