2010-09-28 10 views
12

¿Alguien tiene alguna idea de por qué CGContextFillPath no funcionará en el siguiente fragmento de código? Estoy usando el siguiente código para dibujar a un UIImageView. Está acariciando el camino correctamente, pero ignorando CGContextFillPath.Dibujo de línea CGContext: CGContextFillPath no funciona?

UIGraphicsBeginImageContext(self.frame.size); 
[drawingView.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); 
CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES); 
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); 
CGContextSetRGBFillColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); 
CGContextBeginPath(UIGraphicsGetCurrentContext()); 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), firstMovedTouch.x, firstMovedTouch.y); 
CGContextStrokePath(UIGraphicsGetCurrentContext()); 
CGContextFillPath(UIGraphicsGetCurrentContext()); 
drawingView.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

Respuesta

5

Creo que debe llamar al CGContextClosePath antes de poder completar el camino. No estoy seguro de lo que está tratando de dibujar, pero CGContextFillPath llenará el área dentro de la ruta y solo veo una línea aquí.

+0

Oh ... tienes razón. Estoy usando el código para el dibujo de líneas del dedo del gueto. Pero eso tiene sentido. Este código se activa en touchesMoved: y funciona muy bien para los dibujos lineales, pero tendré que guardar todos los puntos en una matriz, o algo así, para completar la ruta una vez que el usuario levanta el dedo. – RyJ

9

Tuve el mismo problema. He descubierto que no se puede utilizar

CGContextStrokePath(UIGraphicsGetCurrentContext()) 
CGContextFillPath(UIGraphicsGetCurrentContext()) 

consecutivamente: éste no funcionará porque después de cometer el golpe en el camino, se elimina el camino por el contexto.

Así que utilicé el mismo camino dos veces; uno por golpe, el otro por relleno.

// ... create a path of CGMutablePathRef .... 

CGContextSaveGState(context); 
//....fill the path ..... 
CGContextRestoreGState(context); 

CGContextSaveGState (context); 
//....stroke the path ..... 
CGContextRestoreGState(context); 

CFRelease(path); 
+0

Gracias. Eso me salvó después de siglos de problema al disparar – Designer023

+0

Está en los documentos: cuando quiera agregar la ruta a un contexto de gráficos, llame a la función CGContextAddPath. La ruta permanece en el contexto gráfico hasta que Quartz lo pinta. Puede volver a agregar la ruta llamando a CGContextAddPath. –

26

Para resolver este problema, utilice el siguiente código:

CGContextRef context = UIGraphicsGetCurrentContext(); 

CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0); 
CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0); 
CGContextSetLineJoin(context, kCGLineJoinRound); 
CGContextSetLineWidth(context, 8.0); 

CGMutablePathRef pathRef = CGPathCreateMutable(); 

/* do something with pathRef. For example:*/ 
CGPathMoveToPoint(pathRef, NULL, x, y); 
CGPathAddLineToPoint(pathRef, NULL, x, y+100); 
CGPathAddLineToPoint(pathRef, NULL, x+100, y+100); 
CGPathAddLineToPoint(pathRef, NULL, x+100, y); 
CGPathCloseSubpath(pathRef); 

CGContextAddPath(context, pathRef); 
CGContextFillPath(context); 

CGContextAddPath(context, pathRef); 
CGContextStrokePath(context); 

CGPathRelease(pathRef); 
+0

¡Esto ayúdame, gracias! – Christopher

+0

Brillante !!!!! –

18

Creo que la mejor manera de trazo y relleno de su trayectoria es añadir la siguiente línea de código después de que haya terminado entorno en su camino:

CGContextDrawPath(myContext, kCGPathFillStroke); 
+1

Awesome !!!!!!!! –

+1

Genius !!!!!!!!! –

+0

Es simplemente una cuestión de ver el curso CS193P de Stanford en el día ... Creo que fue la segunda o tercera conferencia que presentaron esto. – Mazyod