本文介绍了这个错误是什么无效的上下文0x0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ViewDidLoad中编写了以下代码

  //在加载视图后执行viewDidLoad进行额外的设置,笔尖。 
- (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:@Helveticasize:100.0f]];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(60,200,250,100)];
[label setBackgroundColor:[UIColor clearColor]];
label.font = [UIFont fontWithName:@DBLCDTempBlacksize:50.0];
label.textColor = [UIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];
[label setText:str];

[self.view addSubview:label];
}



在控制台中我看到了

 测试时钟[3286]<错误> ;: CGContextSetStyle:无效的上下文0x0 
测试时钟<错误> ;: CGContextSetFont:无效的上下文0x0
测试时钟CGContextSetTextMatrix:无效的上下文0x0
测试时钟:CGContextSetFontSize:无效的上下文0x0
测试时钟<错误>:CGContextSetTextPosition :invalid context 0x0
测试时钟[3286]<错误> ;: CGContextShowGlyphsWithAdvances:无效的上下文0x0


  CGContextRef上下文=  UIGraphicsGetCurrentContext(); 
if(context){
//做你的东西
}


I wrote the following code in 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];
  }

In Console I saw as

 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
解决方案

You just have to check if context exist before drawing :

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

这篇关于这个错误是什么无效的上下文0x0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:31