Cgcontextsetfillcolorwithcolor: Invalid Context 0X0

CGContextSetFillColorWithColor: invalid context 0x0

Your image.size isn't valid, so UIGraphicsBeginImageContextWithOptions isn't creating a graphics context. Both image.size.width and image.size.height must be positive, finite numbers.

Possibly image itself is nil. When you send the size message to nil, you get back CGSizeZero.

Why do CGContextSetFillColorWithColor and CGContextFillRects both throw invalid context 0x0 error?

You need to get the current context after you begin the image context.

(Apparently, when you are calling this code, before you call UIGraphicsBeginImageContext(), there is no current context. Therefore UIGraphicsGetCurrentContext() returns nil.)

let rect = CGRectMake( 0.0, 0.0, 16.0, 16.0 )
UIGraphicsBeginImageContext( rect.size )

let context = UIGraphicsGetCurrentContext()

CGContextSetFillColorWithColor( context, color.CGColor )
CGContextFillRect( context, rect )

let image = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()

Invalid context 0x0 error when calling UIGraphicsBeginImageContext. Similar error occurs when calling any Core Graphics 'AddPath' functions as well

This error is common.
Some people just ignore it.
It mean that you are calling CG functions without Context.
CG is a C API not object oriented So you need to provide a Context each time.
If you are in a drawRect: method, it is not needed.
So in your fillCircle you have to provide a valid context.
The problem is that when you do bulletColor.set() there is no context.

In your case the best think to do is to force a call of the drawrect: method.
for that you can call this method in the fillCircle function.

self.setNeedsDisplay();

for that you have to handle all of your drawing logic in the drawRect method.

Apple made a Core Graphics guide:
https://developer.apple.com/library/ios/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/Introduction/Introduction.html
It is very interesting.

note:Quartz 2d is Core Graphics.

Hope it helps.

Error: CGContextSetFillColorWithColor: invalid context 0x0

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.

invalid context 0x0 within custom UIView

In the documentation for the draw(_ rect: CGRect) function. (Last paragraph of the "Discussion".)

You should never call this method directly yourself.

If you want to trigger this function you should use...

self.setNeedsDisplay()

Documentation for the draw function.

However

As @vacawama pointed out (and I missed) the draw function will be called after adding your view to the view hierarchy anyway. So you shouldn't need to call it at all here. It will happen automatically for you.

So inside the init methods, just remove the call altogether.

The place to call it again is if the view changes shape for instance or if you want to change the contents of it. i.e. colour, or something.

CGContextSaveGState: invalid context 0x0 (Xcode 7 GM)

This also happens for me on 7 GM, but removing UIViewControllerBasedStatusBarAppearance from Info.plist fixed it for me, as said here.

Update: Warning seems to be gone with iOS 9.2

CGContextSaveGState: invalid context 0x0 after UIGraphicsBeginImageContextWithOptions

(Initially, I thought this was a dupe of How can I fix CGContextRestoreGState: invalid context 0x0. It isn't; the message is different, and this one has an easy fix.)

In the cases where this error is occurring, newSize has a height and/or width of 0 (or negative).

UIGraphicsBeginImageContextWithOptions fails in this case, but it isn't shaped in a way that it can return an error. (There's no return or result.) Thus, you'll get this error instead as soon as you try to draw in the context that you weren't able to create.

To test for this, add a couple lines:

- (UIImage *)myImagePaddedToSize:(CGSize)newSize {
NSParameterAssert(newSize.width > 0);
NSParameterAssert(newSize.height > 0);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);

Give that a try. My guess is that it crashes in a few minutes of testing.

If this is indeed what's happening, you'll want to put in a permanent fix instead like so:

- (UIImage *)myImagePaddedToSize:(CGSize)newSize {
if ((newSize.width <= 0) || (newSize.height <= 0)) {
return nil;
}
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);

The idea is that you can't pad an image to a zero or negative size, so just return nil.



Related Topics



Leave a reply



Submit