在 Cocoa 中以编程方式编辑图像?
我想为我的应用程序设置自定义鼠标光标,并希望通过用自定义颜色替换白色边框来以编程方式将默认光标的颜色更改为自定义颜色。问题是我什至不知道从哪里开始在 Cocoa 中以编程方式编辑图像,因此非常感谢任何帮助!
I want to set a custom mouse cursor for my app and would like to programmatically change the default cursor's color to a custom color by replacing the white border with the custom color. The problem is that I don't even know where to start to programmatically edit images in Cocoa, so any help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 -[NSCursor arrowCursor] 获取默认光标。有了光标后,您可以使用 -[NSCursor image] 获取其图像。您不应该修改另一个对象的图像,因此您应该复制该图像。然后您应该编辑图像,并使用 -[NSCursor initWithImage:hotSpot:] 创建一个新光标。您的代码应如下所示:
您应该能够使用核心图像过滤器将图像的白色替换为自定义颜色。但如果您只是想开始,可以使用 NSReadPixel() 和 NSRectFill 一次为一个像素着色。像使用 NSReadPixel 和 NSRectFill 那样每次绘制一个像素会非常慢,因此您应该这样做只是为了了解所有这些是如何工作的。
You can get the default cursor with -[NSCursor arrowCursor]. Once you have a cursor, you can get its image with -[NSCursor image]. You shouldn't modify another object's image, so you should copy that image. Then you should edit the image, and create a new cursor with -[NSCursor initWithImage:hotSpot:]. Your code should look something like this:
You should be able to replace the white color of the image with a custom color by using a core image filter. But if you just want to get started, you can use NSReadPixel() and NSRectFill to color one pixel at a time. Drawing one pixel a a time like that with with NSReadPixel and NSRectFill will be exceptionally slow, so you should only do that to get a feel for how all of this works.
我的最终代码。这采用标准 IBeam 光标(将鼠标悬停在文本视图上时的光标)并将彩色光标存储在
coloredIBeamCursor
指针中。My final code. This takes the standard IBeam cursor (that one when you hover over a textview) and stores the colored cursor in the
coloredIBeamCursor
pointer.