I am trying draw an random graph which display on imageview, I try this code
-(void)createGraph{
UIGraphicsBeginImageContext(self.drawImage.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 2.0);
int i = 0;
while (i<=20) {
int r = rand() % 100;
CGContextMoveToPoint(context, 20, 320);
CGContextAddLineToPoint(context, linePoint.x+20+i*r, linePoint.y+320-i*r);
CGContextStrokePath(context);
i++;
NSLog(@"random value %d",r);
}
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
}
But its drawing a diagonal on single line. has show in below image
I am expecting to draw a graph line! using coregraphics

First of all, you cannot put
CGContextAddLineToPointinto the loop. Remove (just) the loop, and make another loop in e.g.viewDidLoad, at each iteration, call-(void)createGraphgiving the last point and the next one. Now the last point you will assign toCGContextMoveToPoint, and the next point you will give toCGContextAddLineToPoint. By the way, addCGContextBeginPath(context);right afterCGContextSetLineWidth(context, 2.0);