iOS CGContext在for循环中绘制线图cocos2d

时间:2012-05-08 16:01:38

标签: ios cocos2d-iphone cgcontext

我有一个for循环,在for循环中我执行以下操作,这会在我的图表的背景上创建一个折线图,但是它会在每个背景上显示相同的图像,即使数据是相同的不同。

UIGraphicsBeginImageContext(test.size);

CGContextRef c = UIGraphicsGetCurrentContext();
CGContextFlush(c);

CGContextSetAllowsAntialiasing(c, true);

CGContextTranslateCTM(c, 0, test.size.height);
CGContextScaleCTM(c, 1.0, -1.0);

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();

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

CGColorRef color = CGColorCreate(colorspace, components);

CGContextSetStrokeColorWithColor(c, color);

CGContextMoveToPoint(c, 0, 0);

      if(maxPoints > 0)
      {
          float xSpacing = (float)330.0f/(float)[allScorePointsSubset count];
          int currentPoint;
          float currentX;
          float currentY;

             for (int j = 0; j < [allScorePointsSubset count]; j++) 
             {
                NSString *currentPointString = [allScorePointsSubset objectAtIndex:j];
                currentPoint = (int)[currentPointString intValue];

                if(currentPoint < 0)
                  currentPoint = 0;

                currentX = (j+1)*xSpacing;
                currentY = currentPoint / 100.0f * 200.0f;

                CGContextAddLineToPoint(c, (int)currentX, (int)currentY);

              }

      }

  CGContextStrokePath(c);
  CGColorSpaceRelease(colorspace);
  CGColorRelease(color);

  UIImage *graphImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();


  CCSprite* answerMenuNormalSprite = [CCSprite spriteWithCGImage:graphImage.CGImage key:   [NSString stringWithFormat:@"answerMenuNormalSprite_%f", i]];
            [answerMenuNormalSprite setPosition:ccp(176,66)];


  [[background objectAtIndex:0] addChild:answerMenuNormalSprite z:0 tag:i];

有没有人知道我做错了什么,这是否必须采用以下方法

-(void)draw
{
   [super draw];
}

由于

1 个答案:

答案 0 :(得分:0)

事实证明我的计数器没有正确递增,问题只是在下一行中没有给图像一个唯一的键

CCSprite* answerMenuNormalSprite = [CCSprite spriteWithCGImage:graphImage.CGImage key:   [NSString stringWithFormat:@"answerMenuNormalSprite_%f", i]];