我正在使用 NSImage 的 -lockFocusFlipped: 方法对图像进行一些绘制。我的代码如下所示:
NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(256, 256)];
[image lockFocusFlipped:YES];
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowColor:[NSColor blackColor]];
[shadow setShadowBlurRadius:6.0];
[shadow setShadowOffset:NSMakeSize(0, 3)];
[shadow set];
NSRect shapeRect = NSMakeRect(0, 0, 256, 100);
[[NSColor redColor] set];
NSRectFill(shapeRect);
[image unlockFocus];
该代码在一定程度上有效。我可以确认上下文确实翻转了,因为 [[NSGraphicsContext currentContext] isFlipped]
返回 YES
,而且还因为 shapeRect
绘制在右侧位置(以左上角为原点)。也就是说,NSShadow 似乎不尊重上下文的翻转状态。将阴影偏移设置为 (0, 3)
应该会在上下文翻转时向下移动阴影,但实际上它向上移动(这是在标准非翻转上下文中会发生的情况)。
这个问题似乎是 -lockFocusFlipped
特有的,因为当我使用相同的代码绘制到具有翻转坐标系的 CALayer
时,阴影绘制得很好(尊重翻动)。关于 -lockFocusFlipped 的文档似乎也很模糊。这就是 NSImage
类文档中的全部内容:
准备图像以使用指定的翻转状态接收绘图命令。
我还在 Snow Leopard AppKit 发行说明:
有些情况,例如直接通过 NSLayoutManager 绘图,需要翻转上下文。为了涵盖这种情况,我们添加
- (void)lockFocusFlipped:(BOOL)flipped;
这不会改变图像本身的状态,只会改变焦点锁定的上下文。这意味着 (0,0) 位于左上角,Y 轴正方向在锁定上下文中向下。
在这种情况下,所有文档似乎都没有解释 NSShadow
的行为。通过进一步的测试,似乎 NSGradient 似乎也不尊重 NSImage 使用的绘图上下文的翻转状态。
非常感谢任何见解:-)
I'm using NSImage
's -lockFocusFlipped:
method to do some drawing into an image. My code looks like this:
NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(256, 256)];
[image lockFocusFlipped:YES];
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowColor:[NSColor blackColor]];
[shadow setShadowBlurRadius:6.0];
[shadow setShadowOffset:NSMakeSize(0, 3)];
[shadow set];
NSRect shapeRect = NSMakeRect(0, 0, 256, 100);
[[NSColor redColor] set];
NSRectFill(shapeRect);
[image unlockFocus];
This code works to a certain point. I can confirm that the context is indeed flipped because [[NSGraphicsContext currentContext] isFlipped]
returns YES
, and also because shapeRect
is drawn at the right position (using the top left corner as the origin). That said, the NSShadow
does not seem to respect the flipped status of the context. Setting the shadow offset to (0, 3)
should move the shadow down when the context is flipped, but it actually moves it up (which is what would happen in a standard non-flipped context).
This problem seems specific to -lockFocusFlipped
, because when I'm drawing using this same code into a CALayer
with a flipped coordinate system, the shadow is drawn just fine (respecting the flip). Documentation on -lockFocusFlipped
also seems to be quite vague. This is all it says in the NSImage
class documentation:
Prepares the image to receive drawing commands using the specified flipped state.
And I also found this note in the Snow Leopard AppKit Release Notes:
There are cases, for example drawing directly via NSLayoutManager, that require a flipped context. To cover this case, we add
- (void)lockFocusFlipped:(BOOL)flipped;
This doesn't alter the state of the image itself, only the context on which focus is locked. It means that (0,0) is at the top left and positive along the Y-axis is down in the locked context.
None of the docs seem to explain NSShadow
's behaviour in this case. And through further testing, it seems NSGradient
does not seem to respect the flipped state of the drawing context used by NSImage
either.
Any insight is greatly appreciated :-)
发布评论
评论(1)
来自 NSShadow 类参考:
这就是翻转的最终结果: 向上翻译,向相反方向缩小。
NSGradient 没有这样的声明,所以我建议 提交有关该错误的错误。
From the NSShadow class reference:
And that's what flipping ultimately is: Translate up, scale back the other way.
There's no such statement for NSGradient, so I'd suggest filing a bug about that one.