CGPathApplierFunction的异常

时间:2012-06-25 15:19:49

标签: cocoa cgpath nsbezierpath

我的CGPathRef有5个元素,我想删除起点,并且有一个包含4个元素的路径,其起点与起点的路径末尾相同我想删除。

这是我现在的函数,它会生成异常:

static void constructPath(void *info, const CGPathElement *element) 
{
    NSBezierPath *bezierPath = (NSBezierPath *)info; 
    switch (element->type) {
        case kCGPathElementMoveToPoint:
            // some kind of skipping using points[0] kCGPathElementMoveToPoint
            [bezierPath lineToPoint:element->points[1]]; // line exception in gdb
            [bezierPath lineToPoint:element->points[2]];
            [bezierPath lineToPoint:element->points[3]];
            [bezierPath lineToPoint:element->points[4]];
            break;
        case kCGPathElementAddLineToPoint:

            if ([bezierPath isEmpty]) {
                [bezierPath moveToPoint:element->points[0]]; // the new start, how to go to next from here ?
            } else {

                [bezierPath moveToPoint:element->points[1]];
                [bezierPath moveToPoint:element->points[2]];
                [bezierPath moveToPoint:element->points[3]];
                [bezierPath moveToPoint:element->points[4]];
            }
            break;
        default:
            break;
    }
}

1 个答案:

答案 0 :(得分:0)

只需忽略原始路径的第一个元素(类型为kCGPathElementMoveToPoint)。然后为每一行添加一个新行,除非路径为空,在这种情况下,您需要移动到原始点。

static void constructPath(void *info, const CGPathElement *element)
{
    NSBezierPath *bezierPath = (NSBezierPath *)info;
    if ( kCGPathElementAddLineToPoint == element->type )
    {
        if ( [bezierPath isEmpty] ) {
            [bezierPath moveToPoint:element->points[0]];
        } else {
            [bezierPath lineToPoint:element->points[0]];
        }
    }
}