EDIT: I tried using the push/pop thing, but now it crashes.
I have a feeling what I’m attempting to do is way off.. Is there any way to just get core graphics showing up on the screen? I need something thats able to be updated every frame, like drawing a line between two points that always move around..
Even if someone knows of a complete alternative Ill try it.
in .h
CGContextRef context;
in .m
in init method
int width = 100;
int height = 100;
void *buffer = calloc(1, width * height * 4);
context = CreateBitmapContextWithData(width, height, buffer);
CGContextSetRGBFillColor(context, 1, 1, 1, 1);
CGContextAddRect(context, CGRectMake(0, 0, width, height));
CGContextFillPath(context);
CGImageRef image = CGBitmapContextCreateImage(context);
hud_sprite = [CCSprite spriteWithCGImage:image key:@"hud_image1"];
free(buffer);
free(image);
hud_sprite.anchorPoint = CGPointMake(0, 0);
hud_sprite.position = CGPointMake(0, 0);
[self addChild:hud_sprite z:100];
in a method I call when I want to update it.
int width = 100;
int height = 100;
UIGraphicsPushContext(context);
CGContextClearRect(context, CGRectMake(0, 0, width, height)); //<-- crashes here. bad access...
CGContextSetRGBFillColor(context, random_float(0, 1),random_float(0, 1),random_float(0, 1), .8);
CGContextAddRect(context, CGRectMake(0, 0, width, height));
CGContextFillPath(context);
CGImageRef image = CGBitmapContextCreateImage(context);
UIGraphicsPopContext();
//CGContextRelease(ctx);
[[CCTextureCache sharedTextureCache] removeTextureForKey:@"hud_image1"];
[hud_sprite setTexture:[[CCTextureCache sharedTextureCache] addCGImage:image forKey:@"hud_image1"]];
free(image);
You are calling
UIGraphicsPushContext(context). You must balance this withUIGraphicsPopContext(). Since you’re not callingUIGraphicsPopContext(), you are leavingcontexton UIKit’s graphics context stack, so it never gets deallocated.Also, you are calling
UIGraphicsBeginImageContext, which creates a new graphics context that you later release (correctly) by callingUIGraphicsEndImageContext. But you never use this context. You would access the context by callingUIGraphicsGetCurrentContext, but you never call that.UPDATE
Never call
freeon a Core Foundation object!You are getting a
CGImage(which is a Core Foundation object) with this statement:Then later you are calling
freeon it:You must never do that.
Go read the Memory Management Programming Guide for Core Foundation. When you are done with a Core Foundation object, and you have ownership of it (because you got it from a Create function or a Copy function), you must release it with a Release function. In this case you can use either
CFReleaseorCGImageRelease:Furthermore, you are allocating
bufferusingcalloc, then passing it toCreateBitmapContextWithData(which I guess is your wrapper forCGBitmapContextCreateWithData), and then freeingbuffer. Butcontextkeeps a pointer to the buffer. So after youfree(buffer),contexthas a dangling pointer. That’s why you’re crashing. You cannot freebufferuntil after you have releasedcontext.The best way to handle this is to let
CGBitmapContextCreateWithDatatake care of allocating the buffer itself by passingNULLas the first (data) argument. There is no reason to allocate the buffer yourself in this case.