将 NSImageView 中单击的 NSPoint 转换为正确的底层 NSBitmapImageRep 中的像素坐标
这是我想要完成的任务:我正在开发一个开源 TI 计算器模拟器,目前正在尝试添加皮肤支持。皮肤图像只是一个 NSImageView,其图像设置为皮肤图像。我重写 mouseDown: 方法,并使用 convertPointFromBase: 获取鼠标在 NSImageView 坐标中的位置,其中 locationInWindow 返回值作为要转换的点。
然后,我使用 bestRepresentationForDevice: 和 nil 参数从 NSImageView 获取底层 NSBitmapImageRep。最后,我在 NSBitmapImageRep 上调用 getPixel:atX:y,并使用 point.x 和 point.y 作为位置。
然而,NSImageView 的原点从左下角开始(像往常一样),而 NSBitmapImageRep 的坐标从左上角开始。我似乎找不到明显的方法来弥补这一点,因此按钮点击的点击测试无法正常工作。非常感谢任何帮助。
Here is what I'm trying to accomplish: I'm working on an open source TI calculator emulator where I'm currently trying to add skin support. The skin image is just an NSImageView with its image set to the skin image. I override the mouseDown: method and get the location of the mouse in the NSImageView coordinates using convertPointFromBase: with locationInWindow return value as the point to convert.
I then grab the underlying NSBitmapImageRep from the NSImageView using bestRepresentationForDevice: with a nil argument. Finally I call getPixel:atX:y on the NSBitmapImageRep with point.x and point.y for the location.
However, the origin of the NSImageView begins in the bottom left (as usual), whereas the coordinates for the NSBitmapImageRep begin in the top left. I can't seem to find an obvious way to compensate for this, so the hit testing for button clicks doesn't function properly. Any help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我从未考虑过重写 isFlipped 在我的 NSImageView 子类中返回 YES。这样就解决了问题。
I never considered overriding isFlipped to return YES in my NSImageView subclass. That takes care of the problem.
对我来说似乎很简单,只需根据矩形的高度计算 y 坐标即可:
Seems pretty easy to me, just calculate the y coordinate based on the height of the rect: