Domanda

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"];
È stato utile?

Soluzione

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

Altri suggerimenti

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.

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

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 :)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top