在macOS编程中,我们知道
现在正在使用Quartz API - CGImageCreateWithImageInRect裁剪图像,该图像以矩形作为参数。它的Y起源于可可的滑鼠事件。
所以我在倒置的地方得到庄稼..。
我试着用这段代码在我的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之间
有人能给我指出在这种情况下正确可靠的Y坐标翻转方法吗?谢谢!
下面是GitHub中的示例项目,突出显示问题https://github.com/kamleshgk/SampleMacOSApp

发布于 2018-07-26 14:54:11
正如Charles提到的,您使用的Core Graphics API需要相对于图像(而不是屏幕)的坐标。重要的是将事件位置从窗口坐标转换为与图像位置最接近的视图,然后相对于该视图的边界(而不是帧)将其翻转。所以:
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发布于 2018-07-25 07:20:52
您传递给CGImageCreateWithImageInRect的rect应该是相对于输入图像大小的坐标,而不是屏幕坐标。假设输入图像的大小与您转换到的视图的大小相匹配,您应该能够通过从图像的高度而不是屏幕的高度减去rect的角来实现这一点。
https://stackoverflow.com/questions/51504481
复制相似问题