Pergunta

With the following code:

NSString *imageString = [[NSBundle mainBundle] pathForResource:@"logo64x64" ofType:@"png"];
NSImage *testImage = [[NSImage imageNamed:@"logo64X64"] retain];
NSImage *testImage2 = [[NSImage alloc] initWithContentsOfFile:imageString];

testImage is nil but testImage2 is a NSImage instance. I don't know what's wrong with the code. I'm sure I can find logo64x64.png and logo64x64@2x.png in the resource directory in the bundle. I've also tried imageNamed:@"logo64X64.png" but still getting nil.

Anyone can help?

Foi útil?

Solução

Try

[NSImage imageNamed:@"logo64x64"];

rather than

[NSImage imageNamed:@"logo64X64"];

Note that in the code that succeeded, you used a lowercase x, whereas in the code that failed, you used an uppercase X (logo64X64). NSBundle is case-sensitive, even if the underlying HFS+ file system is only case-preserving. (NSImage's +imageNamed: method uses NSBundle to locate resources).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top