【问题标题】:What this error is invalid context 0x0?这个错误是无效的上下文 0x0 是什么?
【发布时间】:2011-07-06 14:23:27
【问题描述】:

我在ViewDidLoad中写了如下代码

// Implement viewDidLoad to do additional setup after loading the view, typically     from      a nib.
     -(void)viewDidLoad {
    [super viewDidLoad];

NSString *str = [NSString stringWithFormat:@"Veer"];

CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();
CGFloat values[4] = {55.0, 0.0, 0.0, 1.0}; 
CGColorRef glowColor = CGColorCreate(rgbColorspace, values); 
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShadowWithColor( context, CGSizeMake( 5.0, 0.0 ), 20.0f, glowColor);   
[str drawAtPoint:CGPointMake(10.5f, 0.5f) withFont:[UIFont      fontWithName:@"Helvetica" size:100.0f]];    
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60, 200, 250, 100)];
[label setBackgroundColor:[UIColor clearColor]];
label.font=[UIFont fontWithName:@"DBLCDTempBlack" size:50.0];
label.textColor = [UIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];
[label setText:str];

[self.view addSubview:label];
  }

在控制台中我看到了

 Testing Clock[3286] <Error>: CGContextSetStyle: invalid context 0x0
 Testing Clock[3286] <Error>: CGContextSetFont: invalid context 0x0
 Testing Clock[3286] <Error>: CGContextSetTextMatrix: invalid context 0x0
 Testing Clock[3286] <Error>: CGContextSetFontSize: invalid context 0x0
 Testing Clock[3286] <Error>: CGContextSetTextPosition: invalid context 0x0
 Testing Clock[3286] <Error>: CGContextShowGlyphsWithAdvances: invalid context 0x0

【问题讨论】:

  • context 0X0 表示对象已初始化,即它为空。在您的情况下,上下文为空。
  • 但是我应该怎么做才能初始化它?
  • CGContextRef 上下文 = UIGraphicsGetCurrentContext();我写这个是为了上下文

标签: ios cocoa-touch cgcontext quartz-2d


【解决方案1】:

您应该将该自定义绘图代码移出 viewDidLoad 并将其移至 drawRect。

如果您想实现自己的 drawRect 方法,您还需要对视图进行子类化。

查看此示例:http://ykyuen.wordpress.com/2010/08/27/iphone-uiview-with-border/

【讨论】:

  • 即使我做到了,但遇到同样的错误,你试着告诉我你得到了什么,
  • 我已经提到我想在同一个班级做,否则如果你每秒都对对象(用户定义的班级)进行操作,那么它会变得很重,
【解决方案2】:

你只需要在绘制之前检查上下文是否存在:

CGContextRef context = UIGraphicsGetCurrentContext();
if (context) {
    // Do your stuff
}

【讨论】:

    猜你喜欢
    • 2014-08-31
    • 1970-01-01
    • 2013-09-30
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    相关资源
    最近更新 更多