Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-20-2011, 11:27 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 122
architectpianist is on a distinguished road
Default CGContext invalid

Hi,
I'm having a problem with drawing a quad curve to the screen. I'm not using a nib file, just the app delegate and a quartzView subclass of UIView.

Here's my code:

App Delegate m file:
Code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
	window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
	[application setStatusBarStyle:UIStatusBarStyleDefault];
	[window setBackgroundColor:[UIColor blackColor]];
	
	quartzView *quartz = [quartzView new];
	[quartz setNeedsDisplay];
	[window addSubview:quartz];

	UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
	UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(60.0, 320.0, 200.0, 50.0)];
	textLabel.text = @"Hello!";
	textLabel.textColor = [UIColor whiteColor];
	textLabel.font = [UIFont boldSystemFontOfSize:20];
	textLabel.textAlignment = UITextAlignmentCenter;
	textLabel.backgroundColor = [UIColor clearColor];
	[view addSubview:textLabel];
	
	[self.window addSubview:view];
	[self.window makeKeyAndVisible];
    
    return YES;
}
The above code successfully adds a Hello textLabel to the screen.
quartzView:
Code:
//-----------quartzView.h------------
@interface quartzView : UIView {

}

-(void) drawInContext:(CGContextRef)context;

//-----------------quartzView.m-------------------
@implementation quartzView


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

- (void)drawRect:(CGRect)rect 
{
	CGContextRef context = UIGraphicsGetCurrentContext();
	UIGraphicsPushContext(context);
		
	[self drawInContext:context];
	[self setNeedsDisplay];
}

- (void) drawInContext:(CGContextRef)context
{
	float X = 60.0;
	float Y = 150.0;
	
	CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
	CGContextSetLineWidth(context, 2.0);
	
	CGContextBeginPath(context);
	CGContextMoveToPoint(context, X, Y);
	CGContextAddQuadCurveToPoint(context, 160.0, 60.0, 260.0, 150.0);
	CGContextStrokePath(context);
}
No sort of curve shows up on the screen. Also whenever I called drawRect: directly from the appDelegate it gave me a bunch of errors about invalid contexts. Can anybody tell what I'm doing wrong?
architectpianist is offline   Reply With Quote
Old 02-20-2011, 01:30 PM   #2 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 122
architectpianist is on a distinguished road
Default

Everything's fixed now. I edited the generic initWithFrame method of quartzView to this:
Code:
- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
    if (self) 
	{
        CGRect rect = {0.0, 0.0, 320.0, 480.0};
		[self drawRect:rect];
    }
    return self;
}
And that's all it needed, I guess.
Cheers, everybody, and hope this comes in handy for someone, because I was stuck on this for quite a while!
architectpianist is offline   Reply With Quote
Old 02-20-2011, 05:31 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 122
architectpianist is on a distinguished road
Default

Code:
- (void) drawRect:(CGRect)rect
{
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
	CGContextSetLineWidth(context, 3.0);
	CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);     //CGContextSetRGBFillColor doesn't work either
	CGContextBeginPath(context);
	CGContextMoveToPoint(context, 100.0, 60.0);
	CGRect rectangle = {100.0, 60.0, 120.0, 120.0};
	CGContextAddRect(context, rectangle);
	
	CGContextStrokePath(context);
	CGContextFillPath(context);
}
What I'm trying to achieve is a white square with a black outline. The black outline is showing up okay, but the white fill isn't there. It's also giving me
Code:
<Error> CGContextBeginPath: invalid context 0x0
for each time I use CGContext. I don't know why, because the square is showing up anyway. Please help me get my background and get rid of these <Errors>!
architectpianist is offline   Reply With Quote
Old 02-20-2011, 08:18 PM   #4 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 580
ChrisL is on a distinguished road
Default

I'm not sure about the invalid context error, but the problem with the fill not showing up is because the CGContext's current path is cleared as soon as it's filled or stroked. That means you can't call CGContextFillPath after you've already called CGContextStrokePath, as the path as already been cleared by then.

The simplest fix is just to use CGContextDrawPath to fill and stroke the path in one call:
Code:
CGContextDrawPath(context, kCGPathFillStroke);
In general, if you want to draw the same path multiple times, you need to explicitly create a CGPath using CGPathCreateMutable and then add it to the current context when you're ready to draw it with CGContextAddPath. See the section on paths in the Quartz 2D Programming Guide for more info.
ChrisL is offline   Reply With Quote
Reply

Bookmarks

Tags
context, curve, quartz, view

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 378
14 members and 364 guests
cpsclicker, dre, Error404, Gaz, gmarro, jeroenkeij, Kirkout, mottdog, Music Man, PavelMik, teebee74, whitey99, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:11 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0