简体   繁体   中英

why am i getting this error with cgcontext

i am getting this error, usually when i long press in a uitextfield and the magnifying glass appears there are many such errors reported at the same time

eg : CGContextSetStrokeColorWithColor: 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.

the only drawing code i have is as follows

what's wrong with this code

- (void)drawRect:(CGRect)rect
{
// Drawing code

CGContextRef context = UIGraphicsGetCurrentContext(); //context
CGContextBeginPath (context);
CGContextSetLineWidth(context,3);
CGPoint center = CGPointMake(rect.size.width / 2, rect.size.height / 2);//get the view centre

//get the polygon points

CGContextMoveToPoint(context, center.x-70,rect.size.height - 140 );
CGContextAddLineToPoint(context, center.x + 130, rect.size.height - 140);
CGContextAddLineToPoint(context, center.x - 70, 100);
CGContextAddLineToPoint(context, center.x- 70, rect.size.height - 140);
CGContextAddLineToPoint(context, center.x - 50, rect.size.height -140);
CGContextAddLineToPoint(context, center.x - 50, rect.size.height - 160);
CGContextAddLineToPoint(context, center.x - 70, rect.size.height - 160);


CGContextClosePath(context);//close the path
//[[UIColor clearColor] setFill];//set the colour for fill
[[UIColor blackColor] setStroke];//set the line color
CGContextDrawPath (context, kCGPathStroke);//draw

}

Are you the one calling the drawRect: method? You shouldn't do this, you should call setNeedsDisplay , and the Application will call drawRect: at the appropriate time.

If you call the drawRect: method yourself there won't be a CGContext pushed to the context stack so the UIGraphicsGetCurrentContext() will return nil , and you will get the error that you're seeing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM