10

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.

4

1 回答 1

8

element->pointsCGPoint's 的 C 数组,您不能使用该格式说明符将其打印出来。

问题是,没有办法知道这个数组有多少元素(无论如何我都想不出来)。因此,您必须根据操作的类型进行猜测,但大多数都将单个点作为参数(例如 CGPathAddLineToPoint)。

所以打印出来的正确方法是

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

对于将单个点作为参数的路径操作。

希望有帮助!

于 2010-12-18T21:55:05.043 回答