I have an app that loops through a strings of coordinates and draws a shape using UIBezierPath. When I run the app I get the following error:
<Error>: CGContextSetStrokeColorWithColor: invalid context 0x0.
Here is a cut down version of the code I have used to test, and to try and and resolve this error:
UIBezierPath *testPath = [UIBezierPath bezierPath];
[[UIColor blackColor] setStroke];
[[UIColor redColor] setFill];
CGContextRef testPathRef = UIGraphicsGetCurrentContext();
[testPath moveToPoint:CGPointMake(100, 100)];
[testPath addLineToPoint:CGPointMake(200, 100)];
[testPath addLineToPoint:CGPointMake(100, 200)];
[testPath fill];
[testPath stroke];
[testPath setLineWidth:5];
[testPath closePath];
Apple's own documentation seems to suggest that the above is all I need to draw a simple shape and to colour it in, it breifly mentions a 'context' but it being optional. I have looked at other questions involving this kind of error and some seem to suggest an object being null (0x0) but I can't work out what is going wrong.
EDIT: I have created a new classed called CreateButtons that is a sub class of UIView and called it but the drawRect method never seems to get fired.
-(void)drawShape:(NSString*)locationString buttonTagged:(NSInteger)buttonTag{
CreateButtons *createButtonsInstance = [[CreateButtons alloc] init];
}
CreateButtons implementation:
#import "CreateButtons.h"
@implementation CreateButtons
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSLog(@"initWithFrame fired");
}
return self;
}
-(void)drawRect:(CGRect) rect {
NSLog(@"Draw rect fired");
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 150, 150);
CGContextAddLineToPoint(context, 100, 200);
CGContextAddLineToPoint(context, 50, 150);
CGContextAddLineToPoint(context, 100, 100);
CGContextStrokePath(context);
}