これは、私が経験した見知らぬ問題の1つの写真です。
プラグイン アーキテクチャを使用して Core Graphics で描画しています。ホスト アプリは、プラグインに渡されるコンテキストを作成し、プラグインはコンテキストを描画して送り返します。ここでは、複数のオブジェクトを描画するためにこれを複数回実行しました (ただし、デモンストレーションのため、オブジェクトが 1 つだけでも発生します)。結果は変換さCGImageRefs
れてキャンバス (別のCGContext
) に配置され、キャンバスは に変換されてNSImage
表示されます。
ご覧のとおり、問題は、わずかに破損したバージョンが返されることが多いことです。毎回違います。もう 1 つの奇妙な点は、ボックスのアルファ値が 50% であるはずなのに、そうでないことです (2 番目のボックスの上部を除いて、これはほぼ適切な色のように見えます)。興味深いことに、背景 (ホスト アプリ内の Core Graphics で描画) が破損することはないため、CGImage
toNSImage
変換などの問題にはなりません。
さらに奇妙な部分は、プラグインを直接エクスポートしようとしたこと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));}