0

I am new to objective-c and now I faced an issue which I don't understand how to solve. I've read this topic CGContextSetFillColorWithColor: invalid context 0x0 but didn't find an answer.

The problem is that I get 2 errors like this one:

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.

This is my code:

#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
Community
  • 1
  • 1
AOY
  • 355
  • 3
  • 22

1 Answers1

4

In your inspectableDefaults method you should not call [self.fillColor setFill]. The setFill method sets the fill color for the current graphic context, and since you don't have any context there you get this error. Just remove this line and you're good.

Artal
  • 8,933
  • 2
  • 27
  • 30