2010-09-15 313 views
10

我试图使用CGPathApply遍历CGPathRef对象中的每个CGPathElement(主要是编写一种自定义方式来保存CGPath数据)。问题是,每次它调用CGPathApply时,我的程序崩溃都没有任何信息。我怀疑这个问题存在于applier函数中,但我无法说清楚。这里是我的代码示例:如何正确使用CGPathApply

- (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); 
} 

任何想法,为什么调用此方法施加似乎要崩溃?任何帮助是极大的赞赏。

+0

http://www.mlsite.net/blog/?p=1312 – 2011-02-26 00:37:55

+0

看看这里,这是一篇关于如何正确使用CGPathApply的好帖子:http://oleb.net/blog/2012/12 /访问-漂亮印刷-cgpath元素/ – 2013-12-20 08:06:10

回答

8

element->points是一个CGPoint的C数组,你不能用该格式说明符打印出来。

问题是,没有办法知道该数组有多少元素(无论如何我都无法想象)。所以你必须根据操作的类型进行猜测,但是其中大多数都将单点作为参数(例如CGPathAddLineToPoint)。

所以打印出来以适当方式将

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

用于接受单个点作为参数的路径运行。

希望有帮助!