2012-02-29 7 views
5
CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
UIGraphicsBeginImageContext(drawImage.frame.size); 
[drawImage.image drawInRect:CGRectMake(0,0, drawImage.frame.size.width, drawImage.frame.size.height)]; 

CGContextSetRGBStrokeColor(currentContext, 0.0, 0.0, 0.0, 1.0); 
UIBezierPath *path=[self pathFromPoint:currentPoint 
           toPoint:currentPoint]; 

CGContextBeginPath(currentContext); 
CGContextAddPath(currentContext, path.CGPath); 
CGContextDrawPath(currentContext, kCGPathFill); 
drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 

En el código anterior CGContextRef currentContext creado de UIGraphicsGetCurrentContext() y pasarlo a CGContextBeginPath CGContextAddPath CGContextDrawPath CurrentContext tiene parámetro para ellos no su trabajo para mí! Cuando estoy haciendo touchMovie.UIGraphicsGetCurrentContext value pass a CGContextRef no funciona?

Cuando paso directamente UIGraphicsGetCurrentContext() en lugar de currentContext, esto funciona para mí. Quiero saber por qué es así?

@All Por favor, considéreme para este problema.

Respuesta

7

El problema es que currentContext ya no es el contexto actual después de haber iniciado un contexto de imagen:

CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
UIGraphicsBeginImageContext(drawImage.frame.size); 
// Now the image context is the new current context. 

por lo que debe invertir estas dos líneas:

UIGraphicsBeginImageContext(drawImage.frame.size); 
CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

Editar

Como ha señalado Nicolas, debe finalizar el contexto de la imagen cuando ya no la necesite:

drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); // Add this line. 

Editar

Observe también que se está configurando un trazo de color pero el dibujo con llenar comando.

Por lo que debe llamar al método de color apropiado en su lugar:

CGContextSetRGBFillColor(currentContext, 0.0, 1.0, 0.0, 1.0); 
+0

Por otra parte, usted debe terminar el contexto de imagen cuando no lo necesita más a fin de restablecer la pila gráfico en su estado anterior: 'UIGraphicsEndImageContext();' –

+0

@Sch su trabajo para mí Gracias Sch – kiran

+0

lo encuentro otro problema en esto! No puedo establecer el color en este CGContextSetRGBStrokeColor (currentContext, 1.0, 0.0, 0.0, 1.0); si lo configuro en rojo, ¡sigue dibujando negro! – kiran

1

UIGraphicsBeginImageContext crea un nuevo contexto y lo pone en el contexto actual. Por lo que debe hacer

CGContextRef currentContext = UIGraphicsGetCurrentContext();

despuésUIGraphicsBeginImageContext. De lo contrario, obtendrá un contexto, y este contexto se reemplaza inmediatamente por UIGraphicsBeginImageContext.

Cuestiones relacionadas