- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
Mac OS X 10.7.4
我正在绘制到通过 +[NSGraphicsContext graphicsContextWithBitmapImageRep:]
创建的屏幕外图形上下文中。
当我使用 NSBezierPath
类 绘制到此图形上下文中时,一切都按预期工作。
但是,当我使用 CGContextRef
C 函数 绘制到此图形上下文中时,我看不到任何绘制结果。什么都没用。
由于我不会深入的原因,我确实需要使用 CGContextRef
函数(而不是 Cocoa NSBezierPath
类)进行绘制。
下面列出了我的代码示例。我正在尝试绘制一个简单的“X”。一笔使用NSBezierPath
,一笔使用CGContextRef
C函数。第一笔有效,第二笔无效。我做错了什么?
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];
最佳答案
您没有指定如何查看结果。我假设您正在查看 NSImage
img
而不是 NSBitmapImageRep
offscreenRep
。
当您调用 [img lockFocus]
时,您正在将当前 NSGraphicsContext
更改为绘制到 img
中的上下文。因此,NSBezierPath
绘图进入 img
,这就是您所看到的。 CG 绘图进入您没有看到的 offscreenRep
。
与其将焦点锁定在 NSImage 上并在其中绘图,不如创建一个 NSImage 并将 offscreenRep 添加为其代表之一。
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 saveGraphicsState];
[NSGraphicsContext setCurrentContext:g];
// draw first stroke with Cocoa
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
CGContextRef ctx = [g graphicsPort];
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, 0.0, 0.0);
CGContextAddLineToPoint(ctx, imgSize.width, imgSize.height);
CGContextClosePath(ctx);
CGContextStrokePath(ctx);
// done drawing, so set the current context back to what it was
[NSGraphicsContext restoreGraphicsState];
// create an NSImage and add the rep to it
NSImage *img = [[[NSImage alloc] initWithSize:imgSize] autorelease];
[img addRepresentation:offscreenRep];
// then go on to save or view the NSImage
关于objective-c - Mac OS X : Drawing into an offscreen NSGraphicsContext using CGContextRef C functions has no effect. 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10627557/
如何将图形上下文中的绘图作为图像获取? 最佳答案 如果它的图形端口是 CGBitmapContext,您可以从中创建图像。 但这不是您想要依赖的东西。如果上下文的图形端口不是 CGBitmapCont
我正在drawRect方法内绘制一些形状。 注意:我没有使用 NSViews 或 Layers。 我正在将形状点转换为 3D,然后将它们绘制到屏幕上......但它们缺乏深度。 这是围绕 y 轴旋转
我刚刚开始学习 OSX 编程,我偶然发现了一个问题,我不知道是否应该使用 CGContextRef 还是 NSGraphicsContext。 有什么区别? 我几乎不知道 CG 或 NS 之间的区别[
CGContextRef ctx; NSGraphicsContext *nsGraphicsContext; nsGraphicsContext = [NSGraphicsContext graph
我使用[NSGraphicsContext currentContext]获取上下文,如何获取当前应用于它的NSAffineTransform? 我需要这个来实现我自己的转换堆栈,我可以在其中执行 p
所以我尝试使用与 this answer 类似的方法将文本叠加到 PDF 文档上. 我使用一个函数来设置当前的图形上下文,就像它在文档中所说的那样。 let pdfPage: CGPDFPage =
我有一个 CGContext,我可以将其转换为 NSGraphicsContext。我有一个 NSWindow,其中有一个用于上下文的 ClipRect。 我想将 ScrollView 放入上下文中,
在 macOS 10.12 之前,以下代码在后台线程上运行良好: [nsView lockFocusFocus]; ... draw something ... [[NSGraphicsContext
Mac OS X 10.7.4 我正在绘制到通过 +[NSGraphicsContext graphicsContextWithBitmapImageRep:] 创建的屏幕外图形上下文中。 当我使用
我是一名优秀的程序员,十分优秀!