iPhone Rendering Question

Posted by slythic on Stack Overflow See other posts from Stack Overflow or by slythic
Published on 2010-05-15T20:16:54Z Indexed on 2010/05/15 20:24 UTC
Read the original article Hit count: 317

Hi all,

I'm new to iPhone/Objective-C development. I "jumped the gun" and started reading and implementing some chapters from O'Reilly's iPhone Development. I was following the ebook's code exactly and my code was generating the following error:

CGContextSetFillColorWithColor: invalid context
CGContextFillRects: invalid context
CGContextSetFillColorWithColor: invalid context
CGContextGetShouldSmoothFonts: invalid context

However, when I downloaded the sample code for the same chapter the code is different.

Book Code:

- (void) Render {
    CGContextRef g = UIGraphicsGetCurrentContext();
    //fill background with gray
    CGContextSetFillColorWithColor(g, [UIColor grayColor].CGColor);
    CGContextFillRect(g, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height));
    //draw text in black.
    CGContextSetFillColorWithColor(g, [UIColor blackColor].CGColor);
    [@"O'Reilly Rules!" drawAtPoint:CGPointMake(10.0, 20.0) withFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];
}

Actual Project Code from the website (works):

- (void) Render {
    [self setNeedsDisplay]; //this sets up a deferred call to drawRect.
}

- (void)drawRect:(CGRect)rect {
    CGContextRef g = UIGraphicsGetCurrentContext();
    //fill background with gray
    CGContextSetFillColorWithColor(g, [UIColor grayColor].CGColor);
    CGContextFillRect(g, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height));
    //draw text in black.
    CGContextSetFillColorWithColor(g, [UIColor blackColor].CGColor);
    [@"O'Reilly Rules!" drawAtPoint:CGPointMake(10.0, 20.0) withFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];
}

What is it about these lines of code that make the app render correctly?

- (void) Render {
    [self setNeedsDisplay]; //this sets up a deferred call to drawRect.
}

- (void)drawRect:(CGRect)rect {

Thanks in advance for helping out a newbie!

© Stack Overflow or respective owner

Related posts about iphone

Related posts about game-development