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

时间:2010-04-06 04:55:13

标签: iphone objective-c core-graphics

我有我想要开发图形的代码。代码是,

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

我上面的代码在CGPoint point = [[coordinate objectAtIndex:intIndex] CGPointValue]的行中进入调试器。 如何使用上面的代码在图表中绘制线条????????

现在我将上面的代码更改为fllows,

嗨,荣耀的哈克,我已经猜到了上面的句子。 但是,现在我改变了我的代码,

    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 个答案:

答案 0 :(得分:2)

此时您的图形上下文可能没有添加行的路径。尝试在适当的“开始路径”和“关闭路径”函数调用中包装for循环。

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

另外,请注意我在for循环之外移动了“笔画路径”调用,直到你关闭了路径。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

  

我上面的代码在CGPoint point = [[coordinate objectAtIndex:intIndex] CGPointValue]的行中进入调试器。

因为NSStrings不响应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(除非不是图表的水平偏移量),更不用说任何37加上其他偏移的数字。

相反,请使用以下内容为这些数字命名:

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

这样,如果你想改变图形的水平偏移,你不需要追捕每一个最后的相关数字文字;您只需要更改MyGraphOffsetX的定义。

您还可以使用当前变换矩阵来偏移图表,从而消除每次CGContextMoveToPoint调用的添加,并消除您忘记添加的风险。