2012-06-25 32 views
1

我的CGPathRef有5個元素,我想刪除起始點,並有一個4個元素的路徑,其起點與我想要刪除的起點的路徑的末尾相同。與CGPathApplierFunction異常

這是現在我的函數,其產生一個異常:

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; 
    } 
} 
+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]]; 
     } 
    } 
}