问题
I'm trying to load an image using NSImage imageNamed: method but i have no success
i have copied the image in the project folder: Project>Resources and added to the project using: "Add files to project ..."
NSImage* image = [NSImage imageNamed:@"logosH"];
if (image == nil){
NSLog(@"NULL :/");
}
NSImageView *accessory = [[NSImageView alloc] initWithFrame:NSMakeRect(0,0,200,55)];
[accessory setImageScaling:NSScaleToFit];
[accessory setImage:[NSImage imageNamed:@"logosH.png"]];
[myAlert setAccessoryView:accessory];
[myAlert setIcon:[NSImage imageNamed:@"HipArthroplastyTemplating.tiff"]];
[myAlert runModal];
here is the code. i have tried every explanation and solution that i can found in other questions but doesn't work, i'm getting the NULL message in console and the setIcon loads anything
i guess that is something with the bundle or something like that (i'm learning Objective-C so this is new)
I have used the code of an example of cocoa developer but doesn't work, i have tried to load my image in that project and it works there!!! but no in my project :/
any help, any idea? thanks a lot
回答1:
Your image is null because it is not found. This may occur if your image is not copied in your bundle or if the filename is wrong.
Important note: Clean your project before every test to ensure that the resources are updated.
The name of the image should contain the extension too, except for .png where it is not mandatory. If you still have the problem, then it's most likely that the file is not in your bundle. To know if the image is copied to your bundle, click on the image in the Project Navigator (⌘ + 1) and verify in your File Inspector (⌘ + ⌥ + 1) in the Target Membership section that the checkbox right next to your project name is checked.
If the problem is not solved, go in the Project Navigator on your project file (the first file at the top). Be sure that the selection on the left is the name of your project under Target. Then go to the Build Phases tab and open the section Copy Bundle Resources. If your image is not in the list, you have to add it with the + button at the bottom.
来源:https://stackoverflow.com/questions/20444836/nsimage-returns-nil-even-when-png-file-is-in-resource-folder