Resized NSImageView not redrawing

梦想的初衷 提交于 2020-01-03 09:47:07

问题


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?


回答1:


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




回答2:


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.




回答3:


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

Try [imageView setNeedsDisplay: YES].




回答4:


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.



来源:https://stackoverflow.com/questions/7045442/resized-nsimageview-not-redrawing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!