NS图像旋转

发布于 2024-07-25 16:17:08 字数 405 浏览 7 评论 0原文

我有一个窗口,其中包含 NSView 的子类。 在视图中,我放置了一个 NSImage。

我希望能够将图像旋转 90 度,将图像的(新)左上角保持在视图的左上角。 当然,我必须旋转图像,然后平移它以将原点放回原位。

在Carbon中,我发现CGContextRotateCTM可以满足我的需求。 但是,我在 ObjC 中找不到正确的调用。 setFrameCenterRotation 似乎没有做任何事情,并且在 setFrameRotation 中,我似乎无法弄清楚原点在哪里,所以我可以适当地翻译。

似乎在动。 当我调整窗口大小时,它会放置图像(或其一部分,我似乎也有一个奇怪的剪切问题),当我滚动时,它会跳转到不同的(并不总是相同的)位置。

这对任何人都有意义吗?

谢谢

I have a window with an subclass of NSView in it. Inside the view, I put an NSImage.

I want to be able to rotate the image by 90 degrees, keeping the (new) upper left corner of the image in the upper left corner of the view. Of course, I will have to rotate the image, and then translate it to put the origin back into place.

In Carbon, I found CGContextRotateCTM which does what I want . However, I can't find the right call in ObjC. setFrameCenterRotation doesn't seem to do anything, and in setFrameRotation, I can't seem to figure out where the origin is, so I can approprately translate.

It seems to move. When I resize the window it puts the image (or part of it, I seem to have a strange clipping issue as wel) and when I scroll, it jumps to a different (and not always the saem) location.

Does this make sense to anyone?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他夏了夏天 2024-08-01 16:17:08

我在屏幕上旋转我正在使用的应用程序的文本,而 Cocoa (我假设您在问题中指的是 Cocoa 而不是 ObjC)这样做的方法是使用 NSAffineTransform。

下面是一个可以帮助您入门的片段

double rotateDeg = 90;
NSAffineTransform *rotate = [[NSAffineTransform alloc] init];
NSGraphicsContext *context = [NSGraphicsContext currentContext];

[context saveGraphicsState];
[rotate rotateByDegrees:rotateDeg];
[rotate concat];

    /* Your drawing code [NSImage drawAtPoint....]for the image goes here 
       Also, if you need to lock focus when drawing, do it here.       */

[rotate release];
[context restoreGraphicsState];

旋转的数学运算在这里可能会有点棘手,因为上面所做的是旋转您正在绘制的坐标系。 我的90度旋转是逆时针旋转。

I rotate text on the screen for an app I work on and the Cocoa (I assume you mean Cocoa and not ObjC in your question) way of doing this is to use NSAffineTransform.

Here's a snippet that should get you started

double rotateDeg = 90;
NSAffineTransform *rotate = [[NSAffineTransform alloc] init];
NSGraphicsContext *context = [NSGraphicsContext currentContext];

[context saveGraphicsState];
[rotate rotateByDegrees:rotateDeg];
[rotate concat];

    /* Your drawing code [NSImage drawAtPoint....]for the image goes here 
       Also, if you need to lock focus when drawing, do it here.       */

[rotate release];
[context restoreGraphicsState];

The mathematics on the rotation can get a little tricky here because what the above does is to rotate the coordinate system that you are drawing into. My rotation of 90 degrees is a counter-clockwise rotation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文