Ios iPhone CGContext:使用两种不同颜色绘制线条

Ios iPhone CGContext:使用两种不同颜色绘制线条,ios,iphone,objective-c,ios7,Ios,Iphone,Objective C,Ios7,我想用CGContext画一条不同颜色的线 这是我的密码 CGSize size = CGSizeMake(200, 200); UIGraphicsBeginImageContextWithOptions(size, NO, 0); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, 2.0); CGContextMoveToPoint(c

我想用CGContext画一条不同颜色的线

这是我的密码

   CGSize size = CGSizeMake(200, 200);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2.0);
    CGContextMoveToPoint(context, 1, 1);

    CGContextBeginPath(context);
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextAddLineToPoint(context, 50, 50);
    CGContextAddLineToPoint(context, 100, 50);
    CGContextStrokePath(context);

    CGContextBeginPath(context);
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGContextAddLineToPoint(context, 200, 100);
    CGContextStrokePath(context);
我正在尝试此代码,其返回错误:

<Error>: CGContextAddLineToPoint: no current point.
:CGContextAddLineToPoint:没有当前点。

以下代码可能对您有所帮助:

CGSize size = CGSizeMake(200, 200);
//UIGraphicsBeginImageContextWithOptions(size, NO, 0); //comment this line,  if you want a image, see bottom two line.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);

CGContextBeginPath(context);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 1, 1);   //MoveToPoint First
CGContextAddLineToPoint(context, 50, 50);
CGContextAddLineToPoint(context, 100, 50);
CGContextStrokePath(context);

CGContextBeginPath(context);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextMoveToPoint(context, 100, 50);   //MoveToPoint First
CGContextAddLineToPoint(context, 200, 100);
CGContextStrokePath(context);

//UIImage* resultImage = UIGraphicsGetImageFromCurrentImageContext(); //if you just need a image
//UIGraphicsEndImageContext(); //match UIGraphicsBeginImageContextWithOptions, if need a image

在开始新路径之前,您可以使用
CGPathGetCurrentPoint
获取当前点,并使用该点开始新路径我不确定您的问题是什么,但这不仅仅是一个moveToPoint:在启动第二条路径后丢失…谢谢@Volker。当我启动第二条路径时,我在代码中添加了moveToPoint:。它运行良好。@user1831389大卫的解决方案似乎更好。。。
CGSize size = CGSizeMake(200, 200);
//UIGraphicsBeginImageContextWithOptions(size, NO, 0); //comment this line,  if you want a image, see bottom two line.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);

CGContextBeginPath(context);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 1, 1);   //MoveToPoint First
CGContextAddLineToPoint(context, 50, 50);
CGContextAddLineToPoint(context, 100, 50);
CGContextStrokePath(context);

CGContextBeginPath(context);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextMoveToPoint(context, 100, 50);   //MoveToPoint First
CGContextAddLineToPoint(context, 200, 100);
CGContextStrokePath(context);

//UIImage* resultImage = UIGraphicsGetImageFromCurrentImageContext(); //if you just need a image
//UIGraphicsEndImageContext(); //match UIGraphicsBeginImageContextWithOptions, if need a image