Loading Images From a .plist File?

Discussion in 'Mac Programming' started by matthew858, Sep 13, 2008.

  1. macrumors member

    Apr 15, 2008
    Sydney, Australia
    Is it possible to load an image directly from a .plist file? I am an absolute noob, so if I can't do it, what is the best way of loading an image through code but to load different images for different table properties?

    The image type would be a .png or a .jpeg.
  2. macrumors member

    Jan 31, 2005
    Storing the images in the .plist file is not recommended. Just put the files in your bundle and store the relative paths in the .plist file.
  3. thread starter macrumors member

    Apr 15, 2008
    Sydney, Australia
    Being a noob, how do store the relative paths in the .plist file?
  4. macrumors 68040


    Jan 10, 2005
    Dallas, TX
    You should put images in your Resources folder, so you may just want to put the path inside that folder. An example from the stickies app:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    StickiesTemplate.tiff is located at <package root>/Contents/Resources/

    This probably isn't the exact way you'll store your files in the plist, but hopefully you can use this as an example of what to store.

  5. Moderator emeritus


    Jun 15, 2000
    Easiest and most straight forward way of doing this would be to just write a method that returns an image based on a given property, such as a string. Then add all your images to your project and let this method handle choosing which image to use. For example:
    - (NSImage *)imageForProperty:(NSString *)property {
        NSDictionary *imageNames = [NSDictionary dictionaryWithObjectsAndKeys:
            @"img1.png", @"name",
            @"img2.png", @"age",
        return [NSImage imageNamed:[imageNames objectForKey:property]];
    This would look up a given property into a dictionary to find an associated image name, and then create an image from that name.

    Unless you need the images to be changed later on after your program is compiled, I'd use something along these lines.

Share This Page