Mac OS X: Das Zeichnen in einen Offscreen-NSGraphicsContext mit CGContextRef C-Funktionen hat keine Auswirkung. Warum?
Mac OS X 10.7.4
Ich zeichne in einen Offscreen-Grafikkontext, der über erstellt wurde+[NSGraphicsContext graphicsContextWithBitmapImageRep:]
.
Beim Zeichnen in diesen Grafikkontext verwende ich denNSBezierPath
Klassefunktioniert alles wie erwartet.
Beim Zeichnen in diesen Grafikkontext verwende ich jedoch dieCGContextRef
C funktioniertIch sehe keine Ergebnisse meiner Zeichnung. Nichts funktioniert.
Aus Gründen, auf die ich nicht eingehen will, muss ich wirklich mit dem zeichnenCGContextRef
Funktionen (eher als der KakaoNSBezierPath
Klasse).
Mein Codebeispiel ist unten aufgeführt. Ich versuche ein einfaches "X" zu zeichnen. Ein Schlag mitNSBezierPath
, ein Schlaganfall mitCGContextRef
C funktioniert. Der erste Schlag funktioniert, der zweite nicht. Was mache ich falsch?
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];