NSImage旋转 [英] NSImage rotation

查看:1162
本文介绍了NSImage旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我想要将图片旋转90度,将图片的(新)左上角保留在视图的左上角。当然,我必须旋转图像,然后翻译它,把原点放回原处。



在Carbon中,我发现了CGContextRotateCTM,它做了我想要的。但是,我在ObjC中找不到正确的调用。 setFrameCenterRotation似乎没有做任何事情,在setFrameRotation,我似乎无法找出原点在哪里,所以我可以适当翻译。



似乎移动。当我调整窗口大小放置图像(或其中的一部分,我似乎有一个奇怪的剪辑问题),当我滚动,它跳转到一个不同的(而不总是saem)位置。



这对任何人都有意义吗?



感谢

我旋转文本在屏幕上我的工作的应用程序和Cocoa(我假设你的意思是Cocoa,而不是ObjC在你的问题)的方法是使用NSAffineTransform。



以下是一段您应该可以开始使用的代码段。

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

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

/ *你的绘图代码[NSImage drawAtPoint ....]的图像在这里
另外,如果你需要在绘制时锁定焦点,在这里。 * /

[rotate release];
[context restoreGraphicsState];

旋转的数学可能会有点棘手,因为上面的是旋转坐标系统。我的旋转90度是逆时针旋转。


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

解决方案

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.

这篇关于NSImage旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆