【发布时间】:2010-05-15 20:16:54
【问题描述】:
我是 iPhone/Objective-C 开发的新手。我“开枪”并开始阅读和实施 O'Reilly 的 iPhone 开发中的一些章节。我完全按照电子书的代码,我的代码产生了以下错误:
CGContextSetFillColorWithColor: invalid context
CGContextFillRects: invalid context
CGContextSetFillColorWithColor: invalid context
CGContextGetShouldSmoothFonts: invalid context
但是,当我下载同一章节的示例代码时,代码是不同的。
图书代码:
- (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]]];
}
来自网站的实际项目代码(作品):
- (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]]];
}
这些代码行是什么让应用程序正确呈现?
- (void) Render {
[self setNeedsDisplay]; //this sets up a deferred call to drawRect.
}
- (void)drawRect:(CGRect)rect {
提前感谢您帮助新手!
【问题讨论】:
标签: iphone objective-c graphics rendering