调整大小的 NSImageView 不重绘

发布于 2024-11-29 03:01:40 字数 1006 浏览 2 评论 0原文

我有一个 NSImageView,它从应用程序包加载图像。

它的设置如下:

coverImage.image = [NSImage imageNamed:@"themeSignEnd.png"];
coverImage.imageScaling = NSImageScaleNone;
coverImage.imageAlignment = NSImageAlignLeft;

图像显示得很好。

NSImageView 的大小是根据其他几个因素计算的,然后使用以下代码进行设置:

coverImage.frame = CGRectMake((1280 - 869) / 2, 0, 869 * percentage, 800);

第一次完成时(应用程序启动时),效果非常好。

NSImageView 具有一定的大小,并且包含的​​图像在适当的边框处被截断。

不幸的是,当我尝试在应用程序运行时调整 NSImageView 的大小(使用上面相同的代码)时,它不会明显更新。

NSLogging coverImage.frame 显示框架本身已正确更新。然而,视图似乎没有被重新绘制 - 无论是在缩小框架时还是在增长框架时。此外,当我退出并重新启动应用程序时, NSImageView 会绘制正确的边框。

什么会导致这种行为?

NSImageView 是否有可能根本没有重绘?如果是这样,我怎样才能强制重绘?

我错过了 NSImageView 的任何其他怪癖可能会导致这种行为吗?

编辑:

事实证明,只有当ImageFrameStyle属性设置为NSImageFrameNone时,才会发生此错误。如果框架设置为其他任何内容,它就会重新绘制。不幸的是,我们需要在没有框架的情况下显示图像。

有框架和没有框架的 NSImageViews 渲染有什么区别可以解释这一点吗?

I have a NSImageView which loads an image from the application bundle.

It is setup as such:

coverImage.image = [NSImage imageNamed:@"themeSignEnd.png"];
coverImage.imageScaling = NSImageScaleNone;
coverImage.imageAlignment = NSImageAlignLeft;

The image displays just fine.

The size of the NSImageView is calculated based on several other factors and then set using this code:

coverImage.frame = CGRectMake((1280 - 869) / 2, 0, 869 * percentage, 800);

This works perfectly fine the first time it is done (when the application launches).

The NSImageView takes on the size and the image contained is cut off at the appropriate border.

Unfortunately, when I try to resize the NSImageView while the application is running (using the same code above), it won't visibly update.

NSLogging the coverImage.frame shows that the frame itself is updated correctly. However, the view does not seem to be redrawn - neither when shrinking nor when growing the frame. Additionally, when I quit and restart the app, the NSImageView draws the correct border.

What could cause such behavior?

Is it possible that the NSImageView is simply not redrawn? If so, how can I force a redrawing?

Are there any other quirks of NSImageView that I missed which can lead to this behavior?

edit:

It turns out this bug only happens when the ImageFrameStyle property is set to NSImageFrameNone. If the frame is set to anything else, it gets redrawn just fine. Unfortunately, we need the image to be displayed without a frame.

Is there any difference in rendering for NSImageViews with and without frames that could explain this?

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

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

发布评论

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

评论(4

七秒鱼° 2024-12-06 03:01:40

我在 NSImageView imageFrameStyle 属性设置为 NSImageFrameNone 时遇到了非常类似的问题。问题出在自动布局设置中。您可以在这里检查我的问题: NSImageView 不会如果框架样式为 NSImageFrameNone,则缩小图像

I had very similar problem with NSImageView imageFrameStyle property set to NSImageFrameNone. It occurred that the problem was in Auto-Layout settings. You can check my question here: NSImageView won't scale image down if frame style is NSImageFrameNone

月光色 2024-12-06 03:01:40

所以我找到了解决这个问题的方法,但它相当黑客。

正如编辑中提到的,此错误仅在 ImageFrameStyle 设置为 NSImageFrameNone 时发生。当我将 ImageFrameStyle 设置为其他内容,然后调整其大小,然后在调整大小后再次将其设置为 NSImageFrameNone 时,一切正常:

[coverImage setImageFrameStyle: NSImageFramePhoto];
coverImage.frame = CGRectMake((1280 - 869) / 2, 0, 869 * percentage, 800);
[coverImage setImageFrameStyle: NSImageFrameNone];

这可以工作,但不是很漂亮。因此,我将把这个问题再留几天,希望能找到更好的答案。

So I found a way around this, however it is fairly hackish.

As mentioned in the edit, this bug only occurs with ImageFrameStyle set to NSImageFrameNone. When I set the ImageFrameStyle to something else, then resize it, then set it to NSImageFrameNone again after resizing, things work fine:

[coverImage setImageFrameStyle: NSImageFramePhoto];
coverImage.frame = CGRectMake((1280 - 869) / 2, 0, 869 * percentage, 800);
[coverImage setImageFrameStyle: NSImageFrameNone];

This works but isn't very pretty. Thus I'll leave the question open for a couple more days in hope to find a better answer.

绝不放开 2024-12-06 03:01:40

是否有可能 NSImageView 根本没有重绘?如果是这样,我怎样才能强制重绘?

尝试[imageView setNeedsDisplay: YES]

Is it possible that the NSImageView is simply not redrawn? If so, how can I force a redrawing?

Try [imageView setNeedsDisplay: YES].

毁梦 2024-12-06 03:01:40

在使用图层支持的视图时,我出现了一些意想不到的行为。在这些类型的视图中,图层会缓存绘制的内容(包括图像),从而导致一些意外的行为。查看 NSView 的“wantsLayer”属性设置为什么,以便判断是否打开了图层支持。

I have been some unexpected behavior when using layer-backed views. In these types of views, a layer caches what is drawn, including images, resulting in some unexpected behavior. See what the "wantsLayer" property of your NSView is set to, in order to tell if layer-backing is turned on.

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