Cocoa NSPoint 到 Quartz NSPoint - 翻转 Y 坐标
Cocoa NSPoint to Quartz NSPoint - Flip Y cordinate
在 macOS 编程中,我们知道
- Quartz 使用坐标 space,其中原点 (0, 0) 位于主显示器的左上角。增加 y 会下降。
- Cocoa 使用坐标 space,其中原点 (0, 0) 是主显示器的左下角,随着 y 的增加向上。
现在我正在使用 Quartz API - CGImageCreateWithImageInRect 来裁剪图像,它以矩形作为参数。该矩形的 Y 原点来自 Cocoa 的 mousedown 事件。
因此我在倒置的位置得到作物...
我尝试使用此代码翻转 cropRect 中的 Y 坐标
//Get the point in MouseDragged event
NSPoint currentPoint = [self.view convertPoint:[theEvent locationInWindow] fromView:nil];
CGRect nsRect = CGRectMake(currentPoint.x , currentPoint.y,
circleSizeW, circleSizeH);
//Now Flip the Y please!
CGFloat flippedY = self.imageView.frame.size.height - NSMaxY(nsRectFlippedY);
CGRect cropRect = CGRectMake(currentPoint.x, flippedY, circleSizeW, circleSizeH);
但是对于顶部的区域,我错了 FlippedY 坐标。
如果我在视图的顶部边缘附近单击,我得到 flippedY = 510 到 515
在顶部边缘,它应该在 0 到 10 之间:-|
谁能告诉我正确可靠的 Flip 方法
在这种情况下的 Y 坐标?谢谢!
这里是 GitHub 中突出显示问题的示例项目
https://github.com/kamleshgk/SampleMacOSApp
您传递给 CGImageCreateWithImageInRect
的矩形应该是相对于输入图像大小的坐标,而不是屏幕坐标。假设输入图像的大小与您将点转换到的视图的大小相匹配,您应该能够通过从图像的高度而不是屏幕高度减去矩形的角来实现这一点。
正如 Charles 提到的,您使用的核心图形 API 需要相对于图像(而非屏幕)的坐标。重要的是将事件位置从 window 坐标转换为最接近图像位置的视图,然后相对于同一视图的边界(而不是框架)翻转它。所以:
NSView *relevantView = /* only you know which view */;
NSPoint currentPoint = [relevantView convertPoint:[theEvent locationInWindow] fromView:nil];
// currentPoint is in Cocoa's y-up coordinate system, relative to relevantView, which hopefully corresponds to your image's location
currentPoint.y = NSMaxY(relevantView.bounds) - currentPoint.y;
// currentPoint is now flipped to be in Quartz's y-down coordinate system, still relative to relevantView/your image
在 macOS 编程中,我们知道
- Quartz 使用坐标 space,其中原点 (0, 0) 位于主显示器的左上角。增加 y 会下降。
- Cocoa 使用坐标 space,其中原点 (0, 0) 是主显示器的左下角,随着 y 的增加向上。
现在我正在使用 Quartz API - CGImageCreateWithImageInRect 来裁剪图像,它以矩形作为参数。该矩形的 Y 原点来自 Cocoa 的 mousedown 事件。
因此我在倒置的位置得到作物...
我尝试使用此代码翻转 cropRect 中的 Y 坐标
//Get the point in MouseDragged event
NSPoint currentPoint = [self.view convertPoint:[theEvent locationInWindow] fromView:nil];
CGRect nsRect = CGRectMake(currentPoint.x , currentPoint.y,
circleSizeW, circleSizeH);
//Now Flip the Y please!
CGFloat flippedY = self.imageView.frame.size.height - NSMaxY(nsRectFlippedY);
CGRect cropRect = CGRectMake(currentPoint.x, flippedY, circleSizeW, circleSizeH);
但是对于顶部的区域,我错了 FlippedY 坐标。 如果我在视图的顶部边缘附近单击,我得到 flippedY = 510 到 515 在顶部边缘,它应该在 0 到 10 之间:-|
谁能告诉我正确可靠的 Flip 方法 在这种情况下的 Y 坐标?谢谢!
这里是 GitHub 中突出显示问题的示例项目 https://github.com/kamleshgk/SampleMacOSApp
您传递给 CGImageCreateWithImageInRect
的矩形应该是相对于输入图像大小的坐标,而不是屏幕坐标。假设输入图像的大小与您将点转换到的视图的大小相匹配,您应该能够通过从图像的高度而不是屏幕高度减去矩形的角来实现这一点。
正如 Charles 提到的,您使用的核心图形 API 需要相对于图像(而非屏幕)的坐标。重要的是将事件位置从 window 坐标转换为最接近图像位置的视图,然后相对于同一视图的边界(而不是框架)翻转它。所以:
NSView *relevantView = /* only you know which view */;
NSPoint currentPoint = [relevantView convertPoint:[theEvent locationInWindow] fromView:nil];
// currentPoint is in Cocoa's y-up coordinate system, relative to relevantView, which hopefully corresponds to your image's location
currentPoint.y = NSMaxY(relevantView.bounds) - currentPoint.y;
// currentPoint is now flipped to be in Quartz's y-down coordinate system, still relative to relevantView/your image