PDA

View Full Version : Loading Images From a .plist File?




matthew858
Sep 13, 2008, 02:51 AM
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.



Bakerman
Sep 13, 2008, 03:50 AM
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.

matthew858
Sep 13, 2008, 03:53 AM
Being a noob, how do store the relative paths in the .plist file?

lee1210
Sep 13, 2008, 08:31 AM
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">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>tpl</string>
</array>
<key>CFBundleTypeIconFile</key>
<string>StickiesTemplate.tiff</string>


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.

-Lee

kainjow
Sep 13, 2008, 11:30 AM
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",
nil];
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.