NSImageView设置Image失败?
我正在尝试创建一个简单的 ImageView,仅从磁盘加载图像,但这不起作用(我将其放在我的 applicationDidFinishLoading 中):
NSString *file = [@"~/update.png" stringByStandardizingPath];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:file];
[wView setImage:image];
显然未设置 NSImageView 的图像,因为如果我 NSLog [wView图像],我得到(空)。我很确定我在这里犯了一个初学者的错误,但是什么呢?
文件和图像加载良好;如果我将图像绘制到 NSView 上,它会显示得很好。 wView 不是 nil,我尝试使用默认窗口的视图创建另一个项目,将其作为一个自定义 NSImageView 子类,其 initWithFrame: 调用 setImage。还是什么都没有。
I'm trying to create a simple ImageView that just loads an image from disk, but this isn't working (I put it in my applicationDidFinishLoading):
NSString *file = [@"~/update.png" stringByStandardizingPath];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:file];
[wView setImage:image];
Apparently the image for the NSImageView isn't being set, because if I NSLog [wView image], I get (null). I'm pretty sure I'm making a beginner mistake here, but what?
The file and image load fine; if I draw the image to an NSView it shows up fine. wView isn't nil, and I tried making another project with the default window's view a custom NSImageView subclass whose initWithFrame: calls setImage. Still nothing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(OP 这里有一个实际的帐户)
所以事实证明 setImage 不适用于在 IB 中显式构造的 NSImageView,如果你明白我的意思的话。我创建了一个 NSView 子类,在其中构造一个 NSImageView 并调用 setImage,效果很好。真烦人。
(OP here with an actual account)
So it turns out that setImage doesn't work on an NSImageView that's explicitly constructed in IB, if you get what I mean. I made an NSView subclass that constructs an NSImageView inside it and calls setImage, and that worked fine. Really annoying.
最有可能的是,
wView
为零。最常见的原因是,在 nib 中实例化并连接到视图的类也在代码中第二次实例化。Most likely,
wView
is nil. The most common reason for this is when a class that's instantiated in the nib with a connection to a view also gets instantiated a second time in code.