系统偏好设置 NSImage 无法解析

发布于 2025-01-04 06:15:53 字数 1500 浏览 0 评论 0原文

我似乎无法在系统偏好设置项目中解析 NSImages?

该图像是 user.png,位于我的 xcode 项目的主文件夹中。

编辑:我已经包含了源代码的链接。希望有人能够发现问题。

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;
    }
}

另外,指定此路径是可行的,但为什么我必须指定整个路径?

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

I can't seem to get my NSImages to resolve in my System Preferences project?

The image is a user.png that resides in my main folder of my xcode project.

EDIT: I have included a link to the Source Code. Hopefully someone is able to spot the problem.

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"];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吃素的狼 2025-01-11 06:15:53

您可以通过以下方式访问项目文件夹中的图像文件:

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

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];
风苍溪 2025-01-11 06:15:53

请务必使用正确的图像名称。如果图像是“Smiley.png”,则使用“Smiley.png”。此外,您还应该使用带有方法 imageNamed 的扩展。尝试:

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

并仔细检查文件是否确实全部都是小写字母。

Be sure to use the correct name of image. If image is "Smiley.png" then use "Smiley.png". Also you should use extension with method imageNamed. Try:

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

and doublecheck if file is really all in small letters.

匿名的好友 2025-01-11 06:15:53

smiley.png 文件是否显示在 Xcode 项目导航器中?如果不是,它不会被添加到构建的应用程序中。将其从 Finder 拖到导航器中,然后取消选择“将项目复制到目标...”。 Drag

如果它位于导航器中,请检查目标成员资格。单击导航器中的文件并打开实用程序窗格。确保在第一个选项卡上选中您的应用程序的复选框。 目标会员

Does the smiley.png file show up in the Xcode project navigator? 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...". Drag

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. Target Membership

回心转意 2025-01-11 06:15:53

虽然我没有检查您的代码,但我想建议您执行以下步骤:

步骤 1: 在 XIB 中,将 NSImageCell 拖放到 listGender 表列上。

第 2 步:- tableView:objectValueForTableColumn:row:

,当 tableColumn == listGender 时返回 nil

第 3 步: 实现此方法:

– tableView:willDisplayCell:forTableColumn:row:

并在单元格上设置图像,当 tableColumn == listGender

希望这有帮助:)

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.

Step 2: In - tableView:objectValueForTableColumn:row:

return nil, when tableColumn == listGender

Step 3: Implement this method:

– tableView:willDisplayCell:forTableColumn:row:

and set image on the cell, when tableColumn == listGender

Hope this helps :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文