UIGraphicsGetCurrentContext值传递给CGContextRef不起作用?

kir*_*ran 5 iphone core-graphics

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();
Run Code Online (Sandbox Code Playgroud)

在上面CGContextRef currentContext 创建的代码中UIGraphicsGetCurrentContext(),将它传递给 CGContextBeginPath CGContextAddPath CGContextDrawPath currentContext有参数,它对我不起作用!当我在做的时候touchMovie.

当我直接UIGraphicsGetCurrentContext()代替currentContext它为我工作.我想知道为什么会那样?

@All请告诉我这个问题.

sch*_*sch 7

问题是,currentContext在启动图像上下文后,它不再是当前上下文:

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

所以你应该反转这两行:

UIGraphicsBeginImageContext(drawImage.frame.size);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
Run Code Online (Sandbox Code Playgroud)

编辑

正如尼古拉斯所指出的,当你不再需要它时,你应该结束图像上下文:

drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); // Add this line.
Run Code Online (Sandbox Code Playgroud)

编辑

另请注意,您正在设置笔触颜色,但使用填充命令进行绘制.

所以你应该调用适当的颜色方法:

CGContextSetRGBFillColor(currentContext, 0.0, 1.0, 0.0, 1.0);
Run Code Online (Sandbox Code Playgroud)