Mac OS X 10.7.4
経由で作成されたオフスクリーン グラフィックス コンテキストに描画してい+[NSGraphicsContext graphicsContextWithBitmapImageRep:]
ます。
NSBezierPath
classを使用してこのグラフィックス コンテキストに描画すると、すべてが期待どおりに機能します。
しかし、CGContextRef
C 関数を使用してこのグラフィック コンテキストに描画すると、描画の結果が表示されません。何も機能しません。
詳細は省きますが、実際には(CocoaクラスCGContextRef
ではなく) 関数を使用して描画する必要があります。NSBezierPath
私のコードサンプルを以下に示します。単純な「X」を描こうとしています。を使用した一筆書き、 C 関数NSBezierPath
を使用した一筆書き。CGContextRef
最初のストロークは機能しますが、2 番目のストロークは機能しません。私は何を間違っていますか?
NSRect imgRect = NSMakeRect(0.0, 0.0, 100.0, 100.0);
NSSize imgSize = imgRect.size;
NSBitmapImageRep *offscreenRep = [[[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL
pixelsWide:imgSize.width
pixelsHigh:imgSize.height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bitmapFormat:NSAlphaFirstBitmapFormat
bytesPerRow:0
bitsPerPixel:0] autorelease];
// set offscreen context
NSGraphicsContext *g = [NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep];
[NSGraphicsContext setCurrentContext:g];
NSImage *img = [[[NSImage alloc] initWithSize:imgSize] autorelease];
CGContextRef ctx = [g graphicsPort];
// lock and draw
[img lockFocus];
// draw first stroke with Cocoa. this works!
NSPoint p1 = NSMakePoint(NSMaxX(imgRect), NSMinY(imgRect));
NSPoint p2 = NSMakePoint(NSMinX(imgRect), NSMaxY(imgRect));
[NSBezierPath strokeLineFromPoint:p1 toPoint:p2];
// draw second stroke with Core Graphics. This doesn't work!
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, 0.0, 0.0);
CGContextAddLineToPoint(ctx, imgSize.width, imgSize.height);
CGContextClosePath(ctx);
CGContextStrokePath(ctx);
[img unlockFocus];