0

有人建议我使用这行代码从我的资源文件夹/项目包中调用图像。我还看到它在许多不同的网站教程中都以这种方式使用。

NSBundle *mb=[NSBundle mainBundle];


NSString *fp=[mb pathForResource:@"topimage" ofType:@"PNG"];


NSImage *image=[NSImage initWithContentsOfFile:fp];

但是,我收到以下警告:

NSImage 可能无法响应 +initWithContentsOfFile+

NSImage 的文档显示 initWithContentsOfFile 实际上是一种应该有效的方法。我可能在这里缺少什么?

4

2 回答 2

5

你错过了一个+alloc

NSImage* image = [[NSImage alloc] initWithContentsOfFile:fp];

您还可以使用+imageNamed:,它从您的主包中获取图像。

NSImage* image = [NSImage imageNamed:@"topImage.png"];
于 2009-11-02T04:23:47.230 回答
4
于 2009-11-02T05:00:26.450 回答