NSImageView not working?
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.
精彩评论