代码之家  ›  专栏  ›  技术社区  ›  Tirth

如何访问CGPoints数组来绘制图形?

  •  0
  • Tirth  · 技术社区  · 16 年前

    我有一个代码,我想用它来绘制图表。代码是,

    NSArray *coordinate = [[NSArray alloc] initWithObjects: @"42,213", @"75,173", @"108,153", @"141,133", @"174,113", @"207,73", @"240,33", nil];
    CGContextSetRGBFillColor(ctx, 255, 0, 0, 1.0);
    CGContextSetLineWidth(ctx, 8.0);
    for(int intIndex = 0; intIndex < [coordinate count]; fltX1+=33, intIndex++)
    {
        CGContextMoveToPoint(ctx, fltX1+37, fltY2+18);
        CGPoint point = [[coordinate objectAtIndex:intIndex] CGPointValue];
        CGContextAddLineToPoint(ctx, point);
        CGContextStrokePath(ctx);
    }
    

    I上述代码进入调试器的CGPoint point=[[coordinate objectAtIndex:intIndex]CGPointValue]行。

    现在我把上面的代码改成FLOWS,

    但是,现在我改变了我的代码,

        CGContextSetRGBFillColor(ctx, 255, 0, 0, 1.0);
    CGContextSetLineWidth(ctx, 8.0);
    NSArray *coordinate1 = [[NSArray alloc] initWithObjects:@"42",@"75",@"108",@"141",@"174",@"207",@"240",nil];
    NSLog(@"The points of coordinate1: %@", coordinate1);
    NSArray *coordinate2 = [[NSArray alloc] initWithObjects:@"213",@"173",@"153",@"133",@"113",@"73",@"33",nil];
    for(int intIndex = 0; intIndex < [coordinate1 count], intIndex < [coordinate2 count]; fltX1+=33, intIndex++)
    {
        CGContextMoveToPoint(ctx, fltX1+37, fltY2+18);
        NSString *arrayDataForCoordinate1 = [coordinate1 objectAtIndex:intIndex];
        NSString *arrayDataForCoordinate2 = [coordinate2 objectAtIndex:intIndex];
        CGContextAddLineToPoint(ctx, (float *)arrayDataForCoordinate1, (float *)arrayDataForCoordinate2);
        //NSLog(@"CGPoints of drawing the bar: %@", point);
    }
    CGContextClosePath(ctx);    
    CGContextStrokePath(ctx)
    

    3 回复  |  直到 16 年前
        1
  •  0
  •   Peter Hosey    16 年前

    I上述代码进入调试器的CGPoint point=[[coordinate objectAtIndex:intIndex]CGPointValue]行。

    因为字符串不响应 CGPointValue 信息。你可以通过看 the NSString documentation (或者调试器控制台,它将包含一条异常消息来告诉您这一点)。

    我是如何用上面的代码在图中画线的????????

    使用NSScanner自己解析字符串,或者使用 the NSPointFromString function ,或使用NSPoints/CGPoints的C数组(不是NSArray)。

    不相干地说:

    for(int intIndex = 0; intIndex < [coordinate count]; fltX1+=33, intIndex++)
    {
        CGContextMoveToPoint(ctx, fltX1+37, fltY2+18);
    

    不是吗 图的水平偏移),更不用说任何37加上其他偏移的数字了。

    enum {
        MyDistanceBetweenPoints = 33,
        MyGraphOffsetX = 37,
        MyGraphOffsetY = 18,
    };
    

    MyGraphOffsetX

    CGContextMoveToPoint 打电话,消除忘记添加内容的风险。

        2
  •  2
  •   glorifiedHacker    16 年前

    此时,图形上下文可能没有添加行的路径。尝试将for循环包装到适当的“begin path”和“close path”函数调用中。

    CGContextBeginPath(ctx);
    for(int intIndex = 0; intIndex < [coordinate count]; fltX1 += 33, intIndex++) {
        CGContextMoveToPoint(ctx, fltX1+37, fltY2+18);
        CGPoint point = [[coordinate objectAtIndex:intIndex] CGPointValue];
        CGContextAddLineToPoint(ctx, point.x, point.y);
    }
    CGContextClosePath(ctx);
    CGContextStrokePath(ctx);
    

        3
  •  1
  •   drawnonward    16 年前

    尝试[mystringdoublevalue],而不是将(NSString*)类型转换为(float*)。如果有格式为“{x,y}”的字符串,可以使用CGPointFromString直接转换为点。如果可以,请完全避免使用字符串,并将原始数据保留为数字类型。如果必须使用NSArray,则可以是NSNumber对或NSValue的CGPoint。