CGContextRef在插件中被破坏了

时间:2010-05-09 23:58:02

标签: cocoa core-graphics nsbundle

这是我遇到的一个陌生问题的图片:

screenshot

我正在使用插件架构在Core Graphics中绘制内容。主机应用程序创建一个传递给插件的上下文,该上下文将绘制到上下文中并将其发送回去。这里做了多次绘制多个对象(虽然只是为了演示,即使只有一个对象也会发生)。结果将转换为CGImageRefs并放置在画布上(另一个CGContext),该画布又转换为NSImage并显示。

正如您所看到的,问题是它经常会返回一个稍微损坏的版本。每次都不一样。另一个奇怪的事情是盒子应该有50%的alpha,但它们没有(除了第二个盒子的顶部,看起来它是关于那个的正确颜色)。有趣的是,背景(在主机应用程序中的Core Graphics中绘制)并没有被破坏,因此CGImageNSImage转换或其他类似的问题不是问题。

更奇怪的部分是我尝试让插件直接导出NSImage,它具有完全相同的效果,甚至可能稍差一点。在我看来,问题是在插件中使用Core Graphics绘制任何内容。有没有其他人遇到这个,或者更好的是,还有其他人找到了解决方案吗?

如果有帮助,这是主机代码:

 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

插件代码:

 - (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));}

0 个答案:

没有答案
相关问题