如何正确使用CGPathApply [英] How to use CGPathApply properly

查看:287
本文介绍了如何正确使用CGPathApply的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用CGPathApply迭代CGPathRef对象中的每个CGPathElement(主要是编写一种自定义方式来保存CGPath数据)。问题是,每次调用CGPathApply时,我的程序都会崩溃而根本没有任何信息。我怀疑问题在于应用程序功能,但我无法分辨。以下是我的代码示例:

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

为什么对此applier方法的调用似乎崩溃的任何想法?非常感谢任何帮助。

Any ideas as to why the call to this applier method seems to be crashing? Any help is greatly appreciated.

推荐答案

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

element->points is a C array of CGPoint's, you can't print it out with that format specifier.

麻烦是的,没有办法告诉数组有多少元素(无论如何我都无法想到)。因此,您必须根据操作类型进行猜测,但大多数都会将一个点作为参数(例如,CGPathAddLineToPoint)。

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.

希望有所帮助!

这篇关于如何正确使用CGPathApply的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆