0

I am working on an app where I display unsupported (by XCode) file format). So I've subclassed NSBitmapImageRep to display it in a subclass of NSImaageView. I've set it up to be proportionally scallable (up or down). Now I need to add a possibility to get coordinates of pixel in a bitmap. So I've ovveride mouseDown: method:

- (void)mouseDown:(NSEvent *)theEvent
{
    NSLog(@"mouseDown: %ld", [theEvent clickCount]);
    NSPoint point = [theEvent locationInWindow];
    NSLog(@"point x: %f and y: %f", point.x, point.y);    
}

After getting NSPoint I should try to convert it to co-ordinates of a bitmap BUT first I have no idea How to solve the problem that locationInWindow returns NSPoint of a NSImageView, not of the bitmap which is ussually smaller and has unused margins in NSImageView, but I can click on the margin and mouseDown event returns me NSPoint in that margin. Do you have any idea what I shoud do?

4

1 回答 1

0

您需要查看图像矩阵。这是一个将视图坐标映射到图像坐标的矩阵。通常它将是比例和平移的组合。

首先,您需要决定是否要缩放图像以使其完全可见但适合窗口,或者使其完全填满窗口。(或者还有其他选项,例如无论窗口大小始终以 1:1 显示图像。)这将决定图像的比例。

接下来,您需要决定如何定位缩放的图像。如果您将其缩放为始终适合窗口,并且有填充,您是偏爱窗口的左侧和顶部,还是始终尝试居中?如果它被缩放以始终填充窗口,它是在窗口中垂直居中还是水平居中?还是图像的原点总是显示在窗口的左下方?

一旦你弄清楚了比例和平移,你就可以从它们中的 2 个组成一个矩阵。完成此操作后,获取矩阵的逆矩阵,这会将您的视图坐标转换为图像像素。

于 2013-06-01T04:13:51.460 回答