NSImage +imageWithPatternColor: 和 NSGradient - Cocoa/Mac
我正在自定义视图上绘制 NSGradient
,如下所示:
- (void)drawRect: (NSRect)dirtyRect
{
NSGradient* g = [[NSGradient alloc] initWithStartingColor: _color endingColor: [NSColor clearColor]];
[g drawInRect: [self bounds] angle: 90];
}
如果 _color
是普通颜色,例如 [NSColor blueColor]
,则一切都很好。问题是 _color
来自图案图像(在本例中主要是灰色,上面有一些奇特的像素),当发生这种情况时,程序会不断记录此错误:
*** -[NSGradient initWithColors:atLocations:colorSpace:]:颜色 NSPatternColorSpace CGImageSource=0x400367d60" )>无法转换为颜色空间通用 RGB 颜色空间
_color = [NSColor colorWithPatternImage: [NSImage imageNamed: @"mainBG.png"]]
该图像完全不透明,是一个 png 文件。 有什么想法吗?也许我应该更改文件类型?我不知道...
编辑:
如果我像这样定义 _color
:
_color = [[NSColor colorWithPatternImage: [NSImage imageNamed: @"mainBG.tiff"]] colorUsingColorSpace: [NSColorSpace genericRGBColorSpace]]
那么不会显示渐变。没有什么。就像我没有 drawRect:
方法一样。有什么想法吗?
I am drawing on a custom view an NSGradient
like this:
- (void)drawRect: (NSRect)dirtyRect
{
NSGradient* g = [[NSGradient alloc] initWithStartingColor: _color endingColor: [NSColor clearColor]];
[g drawInRect: [self bounds] angle: 90];
}
If _color
is a normal color, for example [NSColor blueColor]
, everything is fine. The problem is that _color
comes from a pattern image (which in this case is mostly grey with some fancy pixels on it), and when that happens the program keeps logging this error:
*** -[NSGradient initWithColors:atLocations:colorSpace:]: the color NSPatternColorSpace CGImageSource=0x400367d60"
)> cannot be converted into color space Generic RGB colorspace
_color = [NSColor colorWithPatternImage: [NSImage imageNamed: @"mainBG.png"]]
The image is completely opaque and is a png file.
Any ideas? perhaps I should change the file type? I don't know...
EDIT:
If I define _color
like this:
_color = [[NSColor colorWithPatternImage: [NSImage imageNamed: @"mainBG.tiff"]] colorUsingColorSpace: [NSColorSpace genericRGBColorSpace]]
then no gradient is displayed. Nothing. Just as if I didn't have the drawRect:
method. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSGradient 不能将图案作为其中一种颜色。这就是异常告诉你的。它仅适用于可转换为 RGB 颜色的 NSColor。如果您想让图案褪色至清晰,则必须采用其他方法。
An NSGradient doesn't work with a pattern as one of the colors. That's what the exception is telling you. It will only work with an NSColor that can be converted to an RGB color. If you want to make your pattern fade to clear, you'll have to do it another way.