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();

In the above code CGContextRef currentContext created of UIGraphicsGetCurrentContext() and pass it to CGContextBeginPath CGContextAddPath CGContextDrawPath currentContext has parameter to them its not working for me! When i am doing touchMovie.

When i pass directly UIGraphicsGetCurrentContext() in place of currentContext its working for me. I want to know why its like that?

@All Please Advice me for this issue.

2 Answers 2

7

The problem is that currentContext is no longer the current context after you start an image context:

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

So you should invert these two lines:

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

Edit

As Nicolas has noted, you should end the image context when you no longer need it:

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

Edit

Also notice that you are setting a stroke color but drawing with the fill command.

So you should call the appropriate color method instead:

CGContextSetRGBFillColor(currentContext, 0.0, 1.0, 0.0, 1.0);
2
  • Furthermore, you should end the image context when you don't need it anymore in order to restore the graphic stack in its previous state: UIGraphicsEndImageContext(); Commented Feb 29, 2012 at 13:32
  • i find another issue on this! i can't able to set color to this CGContextSetRGBStrokeColor(currentContext, 1.0, 0.0, 0.0, 1.0); if i set it to red it's still drawing black!
    – kiran
    Commented Mar 1, 2012 at 1:48
1

UIGraphicsBeginImageContext creates a new context and sets it at the current context. So you should do

CGContextRef currentContext = UIGraphicsGetCurrentContext();

after UIGraphicsBeginImageContext. Otherwise, you fetch a context, and this context is immediately superseded by UIGraphicsBeginImageContext.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.