【问题标题】:iPhone Rendering QuestioniPhone 渲染问题
【发布时间】: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


    【解决方案1】:

    当你在屏幕上绘制东西时,你需要有一个图形上下文(我相信你已经阅读了所有相关内容),但这只是在 Cocoa 调用 -drawRect: 时提供(或多或少),所以您的选项基本上只是在 -drawRect: 中调用渲染,从您尝试执行的操作来看,这没有多大意义,或者让 -render 告诉系统您要绘制视图,这将导致系统(最终)创建一个图形上下文并调用您的 -drawRect:,实际绘图代码需要在其中。否则,将没有要绘制的图形上下文,我相信这就是您的错误所说的。

    (注意:上面的系统意味着 UIKit 和 OS 一样多)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-01
      • 2010-10-19
      • 2016-06-27
      • 2010-11-29
      • 2011-11-24
      相关资源
      最近更新 更多