CGContextRef is corrupted in plugins

Here's a picture of one of the stranger's problems I had:

screenshot

I am using a plugin architecture to draw things in Core Graphics. The host application creates a context which is passed to the plugin, which is pulled into the context and sends it back. Here it has been done multiple times to draw multiple objects (although for demonstration this happens even with one object). The results are converted to CGImageRefs

and placed on the canvas (other CGContext

), which in turn is converted to NSImage

and displayed.

The problem, as you can see, is that it often returns a slightly corrupted version. It is different every time. Another weird thing is that the boxes should have 50% alpha, but they are not (except for the top of the second box, which looks like the right color for that color). Interestingly, the background (drawn in the main chart in the host application) never damaged, so this is not a problem with the conversion CGImage

to NSImage

or something like that.

The even stranger part is that I tried to export the plugin directly NSImage

and it had the same effect, maybe even slightly worse. It seems to me that the problem is drawing something with Core Graphics in the plugin. Does anyone else run into this, or better yet, has anyone else found a solution?

Here is the host code in case it helps:

 CGContextRef tC = [TC CreateBitmapContextWithWidth:720 Height:480]; //Sets up context
 CGContextSetRGBFillColor(tC, 1.0, 1.0, 1.0, 1.0);
 CGContextFillRect(tC, CGRectMake(0, 0, size.width, size.height)); //Draws Background

 for(CDObject *cdo in [[TC DocStorage] Objects]){ //Draws each object
  CGContextRef pluginContext = [TC CreateBitmapContextWithWidth:cdo.width Height:cdo.height]; //Creates context to send to plugin
  [[[TC plugins] objectForKey:[cdo Type]] drawObject:cdo inContext:pluginContext]; //Pass context through appropriate plugin (based on type)
  CGImageRef gci = CGBitmapContextCreateImage(pluginContext); //Create image of context
  CGContextDrawImage(tC, CGRectMake(cdo.x, cdo.y, CGImageGetWidth(gci), CGImageGetHeight(gci)), gci); //Draw image to canvas

  CGImageRelease(gci);
  char *bitmapData = CGBitmapContextGetData(pluginContext);
  CGContextRelease (pluginContext);
  if (bitmapData) free(bitmapData);
 }

 CGImageRef fi = CGBitmapContextCreateImage(tC); //Get image of canvas
 NSImage *tImage = [CGImagetoNSImage(fi) autorelease]; //Use external function to convert to NSImage
 CGImageRelease(fi);
 char *bitmapData = CGBitmapContextGetData(tC);
 CGContextRelease (tC);
    if (bitmapData) free(bitmapData);
 return tImage; //Return the image to be drawn in a view

      

And the plugin code:

 - (void)drawObject:(CDObject*)cdo inContext:(CGContextRef)ctx {
 CGContextSetRGBFillColor(ctx, 1.0, 0.0, 0.0, 0.5);
 CGContextFillRect(ctx, CGRectMake(0, 0, cdo.width, cdo.height));}

      

+2


a source to share





All Articles