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

2 回答 2

2

您需要在方法中的 UIView 子类中调用您的代码drawRect:UIGraphicsGetCurrentContext()当您不在绘图上下文中时,将获得 NULL (0)。

您也可以查看文档

于 2012-04-12T10:47:31.187 回答
0

这是一个非常有用的教程,可以满足您的需要。

http://trailsinthesand.com/exploring-iphone-graphics-part-1/

经历一次..!

于 2012-04-12T10:59:32.863 回答