<Error>:CGContextSetFillColorWithColor:无效上下文 0x0
<Error>: CGContextSetFillColorWithColor: invalid context 0x0
我是 objective-c 的新手,现在我遇到了一个我不知道如何解决的问题。我已阅读此主题 CGContextSetFillColorWithColor: invalid context 0x0 但没有找到答案。
问题是我遇到了 2 个这样的错误:
CGContextSetFillColorWithColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
这是我的代码:
#import "PushButtonView.h"
#import <QuartzCore/QuartzCore.h>
@implementation PushButtonView
@synthesize isAddButton;
@synthesize fillColor;
-(id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:(NSCoder *)aDecoder];
if (!self) {
return nil;
}
[self inspectableDefaults];
return self;
}
-(void)inspectableDefaults{
self.isAddButton = YES;
self.fillColor = [UIColor colorWithRed:0.95 green:0.6 blue:0.5 alpha:1];
[self.fillColor setFill];
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
if (self.isAddButton) {
self.fillColor = [UIColor colorWithRed:0.3 green:0.85 blue:0.91 alpha:1];
} else
self.fillColor = [UIColor colorWithRed:0.95 green:0.6 blue:0.5 alpha:1];
[self.fillColor setFill];
CGContextFillEllipseInRect(context, rect);
//Line characteristics
CGContextSetRGBStrokeColor(context, 1, 1, 1, 1);
CGContextSetLineWidth(context, 4);
// Horizontal line
CGPoint bezierStart = {self.bounds.origin.x + (self.bounds.size.width)/5, self.bounds.size.height/2};
CGPoint bezierEnd = {self.bounds.size.width - (self.bounds.size.width)/5, self.bounds.size.height/2};
CGContextBeginPath(context);
CGContextMoveToPoint(context, bezierStart.x, bezierStart.y);
CGContextAddLineToPoint(context, bezierEnd.x, bezierEnd.y);
CGContextStrokePath(context);
// Vertical line
if (self.isAddButton) {
CGPoint bezier2Start = {self.bounds.size.width/2, self.bounds.size.width - (self.bounds.size.height)/5};
CGPoint bezier2End = {self.bounds.size.width/2, (self.bounds.origin.y + self.bounds.size.height/5)};
CGContextBeginPath(context);
CGContextMoveToPoint(context, bezier2Start.x, bezier2Start.y);
CGContextAddLineToPoint(context, bezier2End.x, bezier2End.y);
CGContextStrokePath(context);
}
[self.fillColor setFill];
}
@end
在您的 inspectableDefaults
方法中,您不应调用 [self.fillColor setFill]
。 setFill
方法为当前图形上下文设置填充颜色,由于您没有任何上下文,所以会出现此错误。只需删除这一行就可以了。
我是 objective-c 的新手,现在我遇到了一个我不知道如何解决的问题。我已阅读此主题 CGContextSetFillColorWithColor: invalid context 0x0 但没有找到答案。
问题是我遇到了 2 个这样的错误:
CGContextSetFillColorWithColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
这是我的代码:
#import "PushButtonView.h"
#import <QuartzCore/QuartzCore.h>
@implementation PushButtonView
@synthesize isAddButton;
@synthesize fillColor;
-(id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:(NSCoder *)aDecoder];
if (!self) {
return nil;
}
[self inspectableDefaults];
return self;
}
-(void)inspectableDefaults{
self.isAddButton = YES;
self.fillColor = [UIColor colorWithRed:0.95 green:0.6 blue:0.5 alpha:1];
[self.fillColor setFill];
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
if (self.isAddButton) {
self.fillColor = [UIColor colorWithRed:0.3 green:0.85 blue:0.91 alpha:1];
} else
self.fillColor = [UIColor colorWithRed:0.95 green:0.6 blue:0.5 alpha:1];
[self.fillColor setFill];
CGContextFillEllipseInRect(context, rect);
//Line characteristics
CGContextSetRGBStrokeColor(context, 1, 1, 1, 1);
CGContextSetLineWidth(context, 4);
// Horizontal line
CGPoint bezierStart = {self.bounds.origin.x + (self.bounds.size.width)/5, self.bounds.size.height/2};
CGPoint bezierEnd = {self.bounds.size.width - (self.bounds.size.width)/5, self.bounds.size.height/2};
CGContextBeginPath(context);
CGContextMoveToPoint(context, bezierStart.x, bezierStart.y);
CGContextAddLineToPoint(context, bezierEnd.x, bezierEnd.y);
CGContextStrokePath(context);
// Vertical line
if (self.isAddButton) {
CGPoint bezier2Start = {self.bounds.size.width/2, self.bounds.size.width - (self.bounds.size.height)/5};
CGPoint bezier2End = {self.bounds.size.width/2, (self.bounds.origin.y + self.bounds.size.height/5)};
CGContextBeginPath(context);
CGContextMoveToPoint(context, bezier2Start.x, bezier2Start.y);
CGContextAddLineToPoint(context, bezier2End.x, bezier2End.y);
CGContextStrokePath(context);
}
[self.fillColor setFill];
}
@end
在您的 inspectableDefaults
方法中,您不应调用 [self.fillColor setFill]
。 setFill
方法为当前图形上下文设置填充颜色,由于您没有任何上下文,所以会出现此错误。只需删除这一行就可以了。