UIGraphicsGetCurrentContext值传递给CGContextRef不起作用?

时间:2012-02-29 13:23:36

标签: 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();

在上面的代码CGContextRef currentContext中创建的UIGraphicsGetCurrentContext()并将其传递给CGContextBeginPath CGContextAddPath CGContextDrawPath currentContext有参数给他们它不适合我!当我在做touchMovie时。

当我直接UIGraphicsGetCurrentContext()代替currentContext为我工作时。我想知道为什么会这样吗?

@All请告诉我这个问题。

2 个答案:

答案 0 :(得分:7)

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

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

所以你应该反过来这两行:

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

修改

正如Nicolas所说,当你不再需要时,你应该结束图像上下文:

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

修改

另请注意,您正在设置描边颜色,但使用填充命令进行绘制。

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

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

答案 1 :(得分:1)

UIGraphicsBeginImageContext创建一个新的上下文并将其设置在当前上下文中。 所以你应该做

CGContextRef currentContext = UIGraphicsGetCurrentContext();

UIGraphicsBeginImageContext之后的

。否则,您获取上下文,此上下文立即被UIGraphicsBeginImageContext取代。