可可+ Quartz 新手:获取 NSImage 和 NSImage 中像素的颜色离屏绘制
我正在尝试为我的 Cocoa(Mac,而不是 iOS)应用程序创建自定义颜色选择器,但遇到了一些麻烦。我对计算机图形和绘图的各种模式非常熟悉,但我对 Cocoa 很陌生,我认为问题基本上是在其多层图形技术中迷失方向。
我想要实现的是一个停靠在更大的侧边栏视图中的颜色选择器,所以我认为最好自己绘制它,而不是使用标准的浮动面板。 (我有充分的理由;无需提及有关偏离 Apple HIG 的警告。)在研究检索 NSImage(在本例中为 colorWheel.PNG)上单击的像素颜色的最佳方法时,我发现它是最好的进入 Core Graphics 并将图像绘制到我自己的位图上下文中。
...这就是我所取得的成功。我已经成功创建了上下文,但我不确定从这里到哪里去,以及这是否太过分了。我想我正在寻找的是对这些类链接在一起的层次结构的可靠解释,以及如何最好地创建我正在寻找的效果。离屏绘图在此应用程序的许多部分都非常有用,我很想更好地理解它。
提前致谢。
I'm attempting to create a custom color picker for my Cocoa (Mac, not iOS) application, and I'm having some trouble. I'm familiar enough with the various patterns of computer graphics and drawing, but I'm new to Cocoa, and I think the issue is basically about getting lost amid its multitiered graphics technologies.
What I'm trying to implement is a color picker that's docked into a larger sidebar View, so I thought it would be best to draw it myself rather than using the standard floating panel. (I have good reason; warnings about diverging from Apple's HIG need not be mentioned.) In researching the best way to retrieve the color of the pixel clicked on an NSImage (in this case, colorWheel.PNG), I have found that it's best to go down into Core Graphics and draw the image into my own bitmap context.
...And that's as far as I've come sucessfully. I've successfully created the context, but I'm unsure where to go from here and whether or not this is overkill. I guess what I'm looking for is a solid explanation of the hierarchy under which these classes link together, and how I might best create the effect I'm looking for. Drawing offscreen will be useful in many parts of this application, and I'd love to understand it better.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
快速解决方案:
无需将其绘制到屏幕外。
只需从 NSImage 创建一个 NSBitmapImageRep 即可。
并使用 colorAtX:y: 检索 NSColor。
当然,您只需要创建一次 NSBitmapImageRep。
然后在每次用户单击或移动鼠标时查找颜色。
在我的应用程序中,这根本不会导致任何性能问题。
使用 NSView 显示 PNG 并检索坐标。
Quick solution:
There is no need to draw it offscreen.
Simply create a NSBitmapImageRep from the NSImage.
And use colorAtX:y: to retrieve the NSColor.
Of course you only need create the NSBitmapImageRep once.
Then lookup the color every time the user clicks of moves the mouse.
In my apps this does not cause any performance problems at all.
Use NSView to display the PNG and retrieve the coordinates.