objective-ccocoansimagesystem-preferences

System Preferences NSImage not resolving


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

Solution

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