2012-04-12 111 views
0

我想在两点之间画线。这是我的代码。但上下文记忆是0在两个不同点之间画线

CGContextRef context = UIGraphicsGetCurrentContext(); 

     CGContextSetLineWidth(context, 2.0); 

     CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); 

     CGFloat components[] = {0.0, 0.0, 1.0, 1.0}; 

     CGColorRef color = CGColorCreate(colorspace, components); 

     CGContextSetStrokeColorWithColor(context, color); 

     CGContextMoveToPoint(context, 0, 0); 
     CGContextAddLineToPoint(context, 300, 400); 

     CGContextStrokePath(context); 
     CGColorSpaceRelease(colorspace); 
     CGColorRelease(color); 
+0

你在用什么方法试试这个?。, – Vignesh 2012-04-12 10:45:18

+0

什么上下文内存?你确定你的'context'变量不是'nil'吗? – Alexander 2012-04-12 10:46:35

+0

可能重复:http://stackoverflow.com/questions/1878821/how-to-draw-line-between-two-points – Devang 2012-04-12 10:48:58

回答

2

您需要在方法drawRect:一个UIView子类中调用你的代码。 UIGraphicsGetCurrentContext()将在您不在绘图环境中时获得NULL(0)。

您可能还会检查docs