Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

man2manno

macrumors member
Original poster
Mar 21, 2009
55
0
Hey guys, I am new to Quartz 2D and have been trying to figure this out. I have a window based app and a UIView as a sub view of it. All I am simply trying to do is draw and line with the following code:

- (void)drawRect:(CGRect)rect {
NSLog(@"I am trying to draw");
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context, 6.0);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextMoveToPoint(context, 100.0f, 100.0f);
CGContextAddLineToPoint(context, 200.0f, 200.0f);
CGContextStrokePath(context);
}

When I run that nothing happens. I have tried to use
[self setNeedsDisplay:YES] and still nothing. Maybe I dont know where to put the setNeedsDisplay? or maybe I am missing a framework? or maybe I am not importing something or making a Quartz 2D delegate? Any help would be greatly appericated!!

Thanks.
 

North Bronson

macrumors 6502
Oct 31, 2007
395
1
San José
Try this, instead of calling:

[1] CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);

Try:

[2] CGContextSetRGBStrokeColor(currentContext, 1, 0, 0, 1);

This function sets the Stroke Color *Space* to RGB, which you might need before you see anything.

You might also replace [1] with:

[3] [[UIColor redColor] set];

which should set the stroke color to red *and* set the color space to RGB. I would rather do this with [2], but it's up to you.
 

firewood

macrumors G3
Jul 29, 2003
8,108
1,345
Silicon Valley
Is your view visible? Are you putting the setNeedsDisplay somewhere where it will actually get called?

(debugger breakpoints are your friends)
 

man2manno

macrumors member
Original poster
Mar 21, 2009
55
0
Yes my view is visible, and where should I be putting my setNeedsDisplay?
I have been putting it in a method when a button is pressed but I get a warning and the program crashes.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Yes my view is visible, and where should I be putting my setNeedsDisplay?
I have been putting it in a method when a button is pressed but I get a warning and the program crashes.

Can you post the code you have. There is no way this should cause a crash...

It should look something like
Code:
[viewPointer setNeedsDisplay];
 

man2manno

macrumors member
Original poster
Mar 21, 2009
55
0
Code:
#import "graph.h"
#import <UIKit/UIKit.h>

@implementation graph

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
		NSLog(@"code started");
    }
    return self;
}



- (IBAction)start:(id)sender {
	NSLog(@"starting");
	

}


- (void)drawRect:(CGRect)rect {
	NSLog(@"I am trying to draw");

	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
	CGContextSetLineWidth(context, 6.0);
	
	CGContextMoveToPoint(context, 100.0f, 100.0f);
	CGContextAddLineToPoint(context, 200.0f, 200.0f);
	CGContextStrokePath(context);
}


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

This is the code where I am just trying to draw the line, Where would I put the setNeedsDisplay?

Thanks alot
 

man2manno

macrumors member
Original poster
Mar 21, 2009
55
0
It Worked but...

Thanks a lot, you were right that works now the drawRect method is being called, but I seem to have run into another problem. My drawRect method is being called, but none of the code actually runs because the debugger says I am getting an error like this:
Code:
2009-05-30 22:19:16.133 Quartz2D[54243:20b] I am trying to draw
Sat May 30 22:19:16 Macintosh-2.local Quartz2D[54243] <Error>: CGContextSetLineWidth: invalid context
Sat May 30 22:19:16 Macintosh-2.local Quartz2D[54243] <Error>: CGContextSetStrokeColorWithColor: invalid context
Sat May 30 22:19:16 Macintosh-2.local Quartz2D[54243] <Error>: CGContextMoveToPoint: invalid context
Sat May 30 22:19:16 Macintosh-2.local Quartz2D[54243] <Error>: CGContextAddLineToPoint: invalid context
Sat May 30 22:19:16 Macintosh-2.local Quartz2D[54243] <Error>: CGContextDrawPath: invalid context
That is what the debugger is saying, the program compiles and runs and drawRect is being called because of the NSLog I have. What does the rest mean?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.