Tracez une ligne à l'iPhone , à l'Exception

Im essayant de dessiner une ligne.

J'ai écrit un code comme ci-dessous.

UIColor *currentColor = [UIColor blackColor];

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
CGContextMoveToPoint(context, startingPoint.x, startingPoint.y);
CGContextAddLineToPoint(context,endingPoint.x , endingPoint.y);
CGContextStrokePath(context);

mais cette montre d'exception comme suit

Sat Aug 21 10:47:20 AAA-rrr-Mac-mini.local Test[2147] <Error>: CGContextSetLineWidth: invalid context 0x0
Sat Aug 21 10:47:20 AAA-rrr-Mac-mini.local Test[2147] <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0
Sat Aug 21 10:47:20 AAA-rrr-Mac-mini.local Test[2147] <Error>: CGContextMoveToPoint: invalid context 0x0
Sat Aug 21 10:47:20 AAA-rrr-Mac-mini.local Test[2147] <Error>: CGContextAddLineToPoint: invalid context 0x0
Sat Aug 21 10:47:20 AAA-rrr-Mac-mini.local Test[2147] <Error>: CGContextDrawPath: invalid context 0x0
  • Vous n'avez pas réellement un contexte graphique. Faites-vous cela à l'intérieur d'un drawRect: ou de quelque autre endroit aléatoire?
  • im en utilisant dans ma propre fonction nommée drawLine:(CGPoint )De:(CGPoint )À
  • doit inclure tout cadre ou un fichier de dessin?
  • vous devriez toujours utiliser drawRect: pour le dessin et l'utilisation setNeedsDisplay pour tout vous rafraîchir voulu.