2

I'm trying to load an image using NSImage imageNamed: method but i have no success

i have copied the image in the project folder: Project>Resources and added to the project using: "Add files to project ..."

    NSImage* image = [NSImage imageNamed:@"logosH"];

    if (image == nil){

        NSLog(@"NULL :/");
    }

    NSImageView *accessory = [[NSImageView alloc] initWithFrame:NSMakeRect(0,0,200,55)];
    [accessory setImageScaling:NSScaleToFit];
    [accessory setImage:[NSImage imageNamed:@"logosH.png"]];
    [myAlert setAccessoryView:accessory];

    [myAlert setIcon:[NSImage imageNamed:@"HipArthroplastyTemplating.tiff"]];

    [myAlert runModal];

here is the code. i have tried every explanation and solution that i can found in other questions but doesn't work, i'm getting the NULL message in console and the setIcon loads anything

i guess that is something with the bundle or something like that (i'm learning Objective-C so this is new)

I have used the code of an example of cocoa developer but doesn't work, i have tried to load my image in that project and it works there!!! but no in my project :/

any help, any idea? thanks a lot

4

1 回答 1

6

您的图像为空,因为未找到。如果您的图像未复制到您的包中或文件名错误,则可能会发生这种情况。

重要说明:在每次测试之前清理您的项目,以确保更新资源。

图片的名称也应该包含扩展名,除了 .png 不是强制性的。如果您仍然有问题,那么很可能该文件不在您的捆绑包中。要知道图像是否已复制到您的包中,请单击项目导航器中的图像(⌘ + 1) 并在目标成员部分的文件检查器(⌘ + ⌥ + 1) 中验证项目旁边的复选框名称被检查。

目标会员

如果问题没有解决,请进入项目导航器中的项目文件(顶部的第一个文件)。确保左侧的选择是Target下的项目名称。然后转到Build Phases选项卡并打开Copy Bundle Resources部分。如果您的图像不在列表中,则必须使用底部的 + 按钮添加它。

在此处输入图像描述

于 2013-12-08T16:51:30.807 回答