更改边界原点 +裁剪图像

发布于 2024-12-27 07:42:54 字数 952 浏览 0 评论 0原文

我是 Cocoa 的新手,我对 NSImage 有一些疑问。

问题1:

更改图像的边界原点似乎没有任何效果。我希望图像是从新设置的原点绘制的,但事实似乎并非如此。我错过了什么吗?

代码:

NSImage* carImage = [NSImage imageNamed:@"car"];

[self.imageView setImage:carImage];

//Following line has no effect:
self.imageView.bounds = CGRectMake(self.imageView.bounds.origin.x + 100, self.imageView.bounds.origin.y, self.imageView.bounds.size.width,self.imageView.bounds.size.height);

注意:imageView是一个IBOutlet

问题2:

我试图裁剪图像,但它似乎没有裁剪图像,我可以看到完整的图像。我缺少什么?

代码:

NSRect sourceRect = CGRectMake(150, 25, 100, 50);
NSRect destRect = CGRectMake(0, 0, 100, 50);

NSImage* carImage = [NSImage imageNamed:@"car"];

[carImage drawInRect:destRect fromRect:sourceRect operation:NSCompositeSourceOver fraction:1.0];

[self.imageView setImage:carImage];

谢谢

I am a newbie to Cocoa, I have a few doubts regarding NSImage.

Question1:

Changing the bounds origin of an image doesn't seem to have any effect. I expected the image to be drawn from the newly set origin but that doesn't seem to the case. Am I missing something ?

code:

NSImage* carImage = [NSImage imageNamed:@"car"];

[self.imageView setImage:carImage];

//Following line has no effect:
self.imageView.bounds = CGRectMake(self.imageView.bounds.origin.x + 100, self.imageView.bounds.origin.y, self.imageView.bounds.size.width,self.imageView.bounds.size.height);

Note: imageView is an IBOutlet

Question2:

I was trying to crop an image, but it doesn't seem to be cropping the image, I can see the complete image. What is that I am missing ?

code:

NSRect sourceRect = CGRectMake(150, 25, 100, 50);
NSRect destRect = CGRectMake(0, 0, 100, 50);

NSImage* carImage = [NSImage imageNamed:@"car"];

[carImage drawInRect:destRect fromRect:sourceRect operation:NSCompositeSourceOver fraction:1.0];

[self.imageView setImage:carImage];

Thanks

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

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

发布评论

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

评论(1

罪#恶を代价 2025-01-03 07:42:54

更改图像的边界原点似乎没有任何效果。 ...

//下面这行没有作用:
self.imageView.bounds = CGRectMake(self.imageView.bounds.origin.x + 100, self.imageView.bounds.origin.y, self.imageView.bounds.size.width,self.imageView.bounds.size.height) ;

那是图像视图,而不是图像。

更改视图边界的效果取决于视图绘制的内容。实际上,这意味着您不应该更改不是您创建的视图类实例的视图的边界,因为您无法准确预测 NSImageView 将如何绘制其图像(大概,因为它是一个控件,所以它涉及它的细胞,但除此之外,我不会依赖)。

更一般地说,更改视图的边界原点是非常罕见的。我不记得曾经做过这件事,我也想不出这样做的理由。更改其边界大小将进行缩放,而不是裁剪。

我试图裁剪图像,但它似乎没有裁剪图像,我可以看到完整的图像。我缺少什么?

[carImage drawInRect:destRect fromRect:sourceRect 操作:NSCompositeSourceOverfraction:1.0];

[self.imageView setImage:carImage];

告诉图像进行绘制不会改变图像的任何内容。它不会“裁剪图像”以使图像随后变小或变大。你只是告诉它画画,仅此而已。

因此,之后的语句将图像视图的图像设置为整个图像,就像您没有告诉图像进行绘制一样,因为告诉它进行绘制没有任何区别。

告诉图像进行绘制的作用正是:它告诉图像进行绘制。只有两个正确的位置可以做到这一点:

  1. 在视图或图像的 lockFocusunlockFocus 消息之间(或者在设置当前 NSGraphicsContext 之后)。
  2. 在视图的 drawRect: 方法中。

在其他地方,您不应该告诉任何 Cocoa 对象进行绘制。

裁剪图像的一种正确方法是创建所需/调整大小的新图像,将焦点锁定在其上,将原始图像的所需部分绘制到其中,然后解锁新图像上的焦点。然后您将获得原始版本和裁剪版本。

另一种正确的方法是创建您自己的自定义图像视图,该视图具有两个属性:一个拥有要绘制的图像,另一个拥有一个矩形。当被告知绘制时,此自定义视图将告诉图像将给定的矩形绘制到视图的边界中。然后,您将始终保留原始图像并仅绘制所需的部分。

Changing the bounds origin of an image doesn't seem to have any effect. …

//Following line has no effect:
self.imageView.bounds = CGRectMake(self.imageView.bounds.origin.x + 100, self.imageView.bounds.origin.y, self.imageView.bounds.size.width,self.imageView.bounds.size.height);

That's an image view, not an image.

The effect of changing the bounds of a view depends on what the view does to draw. Effectively, this means you shouldn't change the bounds of a view that isn't an instance of a view class you created, since you can't predict exactly how an NSImageView will draw its image (presumably, since it's a control, it involves its cell, but more than that, I wouldn't rely on).

More generally, it's pretty rare to change a view's bounds origin. I don't remember having ever done it, and I can't think of a reason off the top of my head to do it. Changing its bounds size will scale, not crop.

I was trying to crop an image, but it doesn't seem to be cropping the image, I can see the complete image. What is that I am missing ?

[carImage drawInRect:destRect fromRect:sourceRect operation:NSCompositeSourceOver fraction:1.0];

[self.imageView setImage:carImage];

Telling an image to draw does not change anything about the image. It will not “crop the image” such that the image will thereafter be smaller or larger. You are telling it to draw, nothing more.

Consequently, the statement after that sets the image view's image to the whole image, exactly as if you hadn't told the image to draw, because telling it to draw made no difference.

What telling an image to draw does is exactly that: It tells the image to draw. There are only two correct places to do that:

  1. In between lockFocus and unlockFocus messages to a view or image (or after setting the current NSGraphicsContext).
  2. Within a view's drawRect: method.

Anywhere else, you should not tell any Cocoa object to draw.

One correct way to crop an image is to create a new image of the desired/adjusted size, lock focus on it, draw the desired portion of the original image into it, and unlock focus on the new image. You will then have both the original and a cropped version.

Another correct way would be to create your own custom image view that has two properties: One owning an image to draw, and the other holding a rectangle. When told to draw, this custom view would tell the image to draw the given rectangle into the view's bounds. You would then always hold the original image and simply draw only the desired section.

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