问题描述:

Im trying to draw a line.

I wrote a code like below.

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

but this shows exception as follows

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

网友答案:

You also need to call CGContextBeginPath(ctx); just before you call CGContextMoveToPoint

- (void) drawRect: (CGRect) rect
{
  UIColor *currentColor = [UIColor blackColor];

  CGContextRef context = UIGraphicsGetCurrentContext(); 
  CGContextSetLineWidth(context, 2.0);
  CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
  CGContextBeginPath(context); // <---- this 
  CGContextMoveToPoint(context, self.bounds.origin.x, self.bounds.origin.y);
  CGContextAddLineToPoint(context, self.bounds.origin.x + self.bounds.size.x, self.bounds.origin.y + self.bounds.size.y);
  CGContextStrokePath(context);
}
网友答案:

You have no valid graphics context. UIGraphicsGetCurrentContext() obviously returned nil.

Where do you want to draw to?

If you want to draw to the screen, then you should implement the drawRect: method of UIView or one of its subclasses and have iOS call that method (by triggering a refresh of part of your screen). Then you will have a valid graphics context during the execution of drawRect:.

If you want to draw to an offscreen pixel map, then you have to create a graphics context yourself with UIGraphicsBeginImageContext or a similar function.

EDIT:

So for drawing into a UIView, you need to create a UIView subclass and override drawRect:

@interfaceMyView : UIView {
}

@end


@implementation MyUIView

- (void) drawRect: (CGRect) rect
{
  UIColor *currentColor = [UIColor blackColor];

  CGContextRef context = UIGraphicsGetCurrentContext(); 
  CGContextSetLineWidth(context, 2.0);
  CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
  CGContextMoveToPoint(context, self.bounds.origin.x, self.bounds.origin.y);
  CGContextAddLineToPoint(context, self.bounds.origin.x + self.bounds.size.x, self.bounds.origin.y + self.bounds.size.y);
  CGContextStrokePath(context);
}

Then you open the XIB file (that you probably already have= in Interface Builder, add a UIView there and select MyUIView as the class (first field on the last tab in the Inspector window).

相关阅读:
Top