可可 - 这个代码使用NSBundle的问题 [英] Cocoa - problem with this code using NSBundle

查看:102
本文介绍了可可 - 这个代码使用NSBundle的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

建议我使用这行代码从我的资源文件夹/项目包调用一个图像。我也看到它在许多不同的网站教程中使用这样。

It was suggested that I use this line of code to call an image from my resources folder/project bundle. I also see it being used exactly like this on many different website tutorials.

NSBundle *mb=[NSBundle mainBundle];


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


NSImage *image=[NSImage initWithContentsOfFile:fp];

然而,我收到以下警告:

HOWEVER, I am receiving the following warning:


NSImage可能不会响应+ initWithContentsOfFile +

NSImage may not respond to +initWithContentsOfFile+

NSImage的文档显示initWithContentsOfFile一个应该工作的方法。

The documentation for NSImage shows that initWithContentsOfFile is in fact a method that should work. What might I be missing here?

推荐答案

您缺少一个 + alloc

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

您也可以使用 + imageNamed:从您的主包中提取图片。

You can also use +imageNamed:, which fetches images from your main bundle.

NSImage* image = [NSImage imageNamed:@"topImage.png"];

这篇关于可可 - 这个代码使用NSBundle的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆