有人建议我使用这行代码从我的资源文件夹/项目包中调用图像。我还看到它在许多不同的网站教程中都像这样使用。
NSBundle *mb=[NSBundle mainBundle];
NSString *fp=[mb pathForResource:@"topimage" ofType:@"PNG"];
NSImage *image=[NSImage initWithContentsOfFile:fp];
但是,我收到以下警告:
NSImage may not respond to +initWithContentsOfFile+
NSImage 的文档显示 initWithContentsOfFile 实际上是一个应该有效的方法。我在这里可能缺少什么?
最佳答案
您缺少一个+alloc
NSImage* image = [[NSImage alloc] initWithContentsOfFile:fp];
您还可以使用+imageNamed:
,它从主包中获取图像。
NSImage* image = [NSImage imageNamed:@"topImage.png"];
关于Cocoa - 使用 NSBundle 的此代码存在问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1659407/