如何正确使用CGPathApply

2024-04-20

我尝试使用 CGPathApply 迭代 CGPathRef 对象中的每个 CGPathElement (主要是编写一种自定义方法来保存 CGPath 数据)。问题是,每次调用 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);
}

关于为什么调用此应用程序方法似乎崩溃的任何想法?任何帮助是极大的赞赏。


element->points是一个 C 数组CGPoint's,您无法使用该格式说明符将其打印出来。

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

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

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

用于以单个点作为参数的路径操作。

希望有帮助!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何正确使用CGPathApply 的相关文章

随机推荐