系统偏好设置 NSImage 无法解析
我似乎无法在系统偏好设置项目中解析 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过以下方式访问项目文件夹中的图像文件:
You can reach image file in your project folder by this way:
请务必使用正确的图像名称。如果图像是“Smiley.png”,则使用“Smiley.png”。此外,您还应该使用带有方法
imageNamed
的扩展。尝试:并仔细检查文件是否确实全部都是小写字母。
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:and doublecheck if file is really all in small letters.
smiley.png
文件是否显示在 Xcode 项目导航器中?如果不是,它不会被添加到构建的应用程序中。将其从 Finder 拖到导航器中,然后取消选择“将项目复制到目标...”。如果它位于导航器中,请检查目标成员资格。单击导航器中的文件并打开实用程序窗格。确保在第一个选项卡上选中您的应用程序的复选框。
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...".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.
虽然我没有检查您的代码,但我想建议您执行以下步骤:
步骤 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 :)