views:

248

answers:

3

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] : CGContextSetLineWidth: invalid context 0x0 Sat Aug 21 10:47:20 AAA-rrr-Mac-mini.local Test[2147] : CGContextSetStrokeColorWithColor: invalid context 0x0 Sat Aug 21 10:47:20 AAA-rrr-Mac-mini.local Test[2147] : CGContextMoveToPoint: invalid context 0x0 Sat Aug 21 10:47:20 AAA-rrr-Mac-mini.local Test[2147] : CGContextAddLineToPoint: invalid context 0x0 Sat Aug 21 10:47:20 AAA-rrr-Mac-mini.local Test[2147] : CGContextDrawPath: invalid context 0x0

A: 

I guess you should use

void UIGraphicsBeginImageContext (
   CGSize size
);

where size is the size of your view I think

rano
This is wrong. This creates an off-screen image context.
St3fan
What do you mean by 'off-screen'?
rano
Please explain with a small example.
new_programmer
A: 

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

Codo
Can you give a small example.
new_programmer
I can give you an example if you answer where you want to draw to.
Codo
I want to draw a line in UIView
new_programmer
+1  A: 

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