简体   繁体   English

系统偏好设置NSImage无法解析

[英]System Preferences NSImage not resolving

I can't seem to get my NSImages to resolve in my System Preferences project? 我似乎无法在系统偏好设置项目中解析NSImages?

The image is a user.png that resides in my main folder of my xcode project. 该图像是一个user.png文件,位于我的xcode项目的主文件夹中。

EDIT: I have included a link to the Source Code . 编辑:我已经包含了到源代码的链接。 Hopefully someone is able to spot the problem. 希望有人能够发现问题。

PersonController.m PersonController.m

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    Person *person = [personsList objectAtIndex:row];

    if (tableColumn == listGender) {
        if ([person.gender isEqualToString: @"m"]) {
            /****************************
             *  Method - 1: Doesn't work
             ****************************/
            NSImage *image = [NSImage imageNamed:@"user.png"];
            NSLog(@"image is valid? %@", [image isValid] ? @"yes" : @"no");
            return image;


//            return @"male";
        }
        else {
            /****************************
             *  Method - 1: Doesn't work
             ****************************/
            NSImage *image = [NSImage imageNamed:@"user_female.png"];
            NSLog(@"image is valid? %@", [image isValid] ? @"yes" : @"no");

            return image;
//            return @"female";
        }
    }
    else if (tableColumn == listName) {
        return [person valueForKey:@"name"];
    }
    else {
        return nil;
    }
}

Also, specifying this path works, but why do I have to specify the entire path? 另外,指定此路径有效,但是为什么必须指定整个路径?

NSImage *myImage = [[NSImage alloc] initByReferencingFile:@"/Users/coderama/sandbox/Persons/user.png"];

You can reach image file in your project folder by this way: 您可以通过以下方式访问项目文件夹中的图像文件:

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"smiley" ofType:@"png"];
NSImage *image = [[NSImage alloc] initWithContentsOfFile: imagePath];
NSImageCell *cell = [[NSImageCell alloc] init];
[cell setImage:image];

Be sure to use the correct name of image. 确保使用正确的图像名称。 If image is "Smiley.png" then use "Smiley.png". 如果图像是“ Smiley.png”,则使用“ Smiley.png”。 Also you should use extension with method imageNamed . 另外,您还应该将扩展名与imageNamed方法一起使用。 Try: 尝试:

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

and doublecheck if file is really all in small letters. 并仔细检查文件是否全部用小写字母表示。

Does the smiley.png file show up in the Xcode project navigator? Xcode项目导航器中是否显示smiley.png文件? If it isn't it won't get added to the built application. 如果不是,它将不会添加到已构建的应用程序中。 Drag it into the navigator from Finder and deselect "Copy Items into destinations...". 将其从Finder拖到导航器中,然后取消选择“将项目复制到目的地...”。 拖动

If it is in the Navigator check the Target Membership. 如果在导航器中,请检查目标成员资格。 Click the file in the navigator and open the Utilities Pane. 单击导航器中的文件,然后打开“实用工具”窗格。 Make sure the checkbox is checked for your application on the first tab. 确保在第一个选项卡上为您的应用程序选中了该复选框。 目标会员

Though I have not checked your code I would like to suggest you these steps: 尽管我尚未检查您的代码,但我还是建议您执行以下步骤:

Step 1: In XIB drag and drop NSImageCell onto listGender table column. 步骤1:在XIB中,将NSImageCell拖放到listGender表列上。

Step 2: In - tableView:objectValueForTableColumn:row: 步骤2:- tableView:objectValueForTableColumn:row:

return nil, when tableColumn == listGender 当tableColumn == listGender时返回nil

Step 3: Implement this method: 步骤3:实施此方法:

– tableView:willDisplayCell:forTableColumn:row:

and set image on the cell, when tableColumn == listGender 并在tableColumn == listGender时在单元格上设置图像

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM