开发者

NSImageView not working?

开发者 https://www.devze.com 2023-02-24 05:37 出处:网络
I am trying put an image in a view. This is my code: NSImageView *imView = [[NSImageView alloc] initWithFrame:NSMakeRect(0, 0, 480, 360)];

I am trying put an image in a view. This is my code:

NSImageView *imView = [[NSImageView alloc] initWithFrame:NSMakeRect(0, 0, 480, 360)];
    NSImage *myImage = [[NSImage alloc] initByReferencingFile:@"image.png"];
    [imView setImage:myImage];
    [t开发者_开发问答heView addSubview:imView];
    [window setContentView:theView];

But the window is blank(image doesn't get drawn). If I place a textfield in the view for example.. it works perfectly, but not NSImageView...I checked the console and there's no warning/error messages... Thanks!


Since [imView isValid] returns false, this means that NSImage hasn’t been able to create an image based on that file. A common reason is that the file couldn’t been located. From the documentation of -initByReferencingFile:,

filename A full or relative path name specifying the file with the desired image data. Relative paths must be relative to the current working directory.

This means that the behaviour of -initByReferencingFile: depends on the current directory, and ideally you should pass the full path name.

A better solution is to use +imageNamed: instead because of two reasons: it uses an image cache and it is able to look for a file inside the application bundle.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号