如何正确使用CGPathApply
我尝试使用 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);
}
关于为什么调用此应用程序方法似乎崩溃的任何想法?非常感谢任何帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
element->points
是CGPoint
的 C 数组,您无法使用该格式说明符将其打印出来。问题是,没有办法知道数组包含多少个元素(无论如何我也想不到)。因此,您必须根据操作类型进行猜测,但大多数操作都将单个点作为参数(例如 CGPathAddLineToPoint)。
因此,打印它的正确方法是
使用单个点作为参数的路径操作。
希望有帮助!
element->points
is a C array ofCGPoint
'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
for a path operation that takes a single point as an argument.
Hope that helps!