My image is not found in Resources, only absolute path works

Discussion in 'Mac Programming' started by rezeda, Dec 8, 2008.

  1. rezeda macrumors newbie

    Joined:
    Nov 11, 2008
    #1
    Hi everyone,
    I have the following problem with loading an image in a cocoa/ obj c application.
    Code that works:

    NSImage *photoImage = [[NSImage alloc] initWithContentsOfFile:mad:"/Users/oana/Project/pagePhoto.jpg"];

    Code that doesn't work:

    NSString imageName = [[NSBundle mainBundle] pathForResource:mad:"pagePhoto" ofType:mad:"jpg"];
    //NSImage *photoImage = [[NSImage alloc] initWithContentsOfFile:imageName];
    //no point in calling the previus line since imageName is allways nil in my case

    I have added pagePhoto.jpg to the Resources folder in xcode, and I can see it in the build directory, in the resources.

    What have I missed? Why only absolute path works and I cannot load the image from resources?

    Thank you for your replies!
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Sounds like something else is going on. You've triple checked that the file is being copied into the Resources folder? Did you try a Clean on the project? Are you sure the extension is .jpg - it could be something else but be hidden in the Finder.
     
  3. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    The absolute path looks like you are looking for the jpeg file in your source directory. That cannot work if you give the application to anyone else, because obviously they won't have your source directory. At some point in the build process that image has to be copied into the application bundle. In your project, check the "Copy Bundle Resources" build phase for your target, that is where I would expect an entry for your pagePhoto.jpg file.
     
  4. rezeda thread starter macrumors newbie

    Joined:
    Nov 11, 2008
    #4
    When I added the jpeg file to Resources, it appeared also under Copy Bundle Resources. So it is sure that the file is present in the resources folder in the application bundle.
    The thing is this line:
    NSString imageName = [[NSBundle mainBundle] pathForResource:mad:"pagePhoto" ofType:mad:"jpg"];
    still doesn't work!

    I have tried the exact code in a new application and it works.
    I am currently developing a PDE (printer driver extension) and I have started from OutputBins2PDE sample code from apple.
    I cannot figure it out why in this sample project the jpeg file cannot be found in resources and in a new application it works fine.

    Thanks again for the support!
     
  5. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #5
    Not at all. Since your code doesn't work, something is clearly not the way you think it is. Does the file appear under "Copy Bundle Resources" now? Did you check it? When you open the package, is the file where you think it would be? Don't assume, check it. What do the NSBundle methods bundlePath and resourcePath return?
     
  6. rezeda thread starter macrumors newbie

    Joined:
    Nov 11, 2008
    #6
    Thank you for the help!
    I run bundlePath and resourcePath and they pointed to the TextEdit bundle instead of my bundle, since the plugin I'm making is not the mainBundle!
    My code works fine now!
    Things are also clear in my head now!
     
  7. Sayer macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #7
    Yeah for plugins you can easily do:

    Code:
    [[NSBundle bundleForClass:[self class]]
    Instead of mainBundle to get the bundle for the plugin itself.
     

Share This Page