2012-02-22 18 views
19

Necesito dibujar un hexágono y rellenarlo con una construcción de color con una imagen como patrón. que hice:CoreGraphics FillPath And Stroke Path

CGContextSaveGState(context); 
CGContextSetLineCap(context, kCGLineCapRound); 
CGContextSetFillColorWithColor(context, [[UIColor colorWithPatternImage:[UIImage imageNamed:@"patternerba.png"]] CGColor]); 
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]); 
CGContextSetLineWidth(context, 3.0); 
// drawing hexagon here... 
CGContextStrokePath(context); 
CGContextFillPath(context); 
[[NSString stringWithFormat:@"Foo"] drawAtPoint:innerRect.origin withFont:[UIFont fontWithName:@"Helvetica" size:16]]; 
CGContextRestoreGState(context); 

embargo, dependiendo de la orden de CGContextStrokePath y CGContextFillPath, consigo un hexágono limita pero no llena o llena pero no confinado. ¿Cómo puedo arreglar esto?

+0

aumentar el ancho en CGContextSetLineWidth .. –

Respuesta

31

Trate

CGContextDrawPath(context, kCGPathFillStroke); 

En lugar de

CGContextStrokePath(context); 
CGContextFillPath(context); 
+0

Funciona bien. Gracias. – IssamTP

+0

Usted hizo mi día. Gracias –

0

O bien utilizar

CGContextDrawPath(context, kCGPathFillStroke); 

como SCH sugirió, o dibujar el hexágono de nuevo antes de llamar FillPath. StrokePath y FillPath eliminan la ruta que ha agregado al contexto, por lo tanto, la próxima llamada fallará silenciosamente sin una ruta.

CGPathRef path = /* drawing hexagon here */; 
CGContextAddPath(context, path); 
CGContextStrokePath(context); 
CGContextAddPath(context, path); 
CGContextFillPath(context); 

Nota: los dos segmentos de código no son equivalentes y arrojan resultados diferentes.