Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

matthew858

macrumors member
Original poster
Apr 15, 2008
97
0
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.
 

Bakerman

macrumors member
Jan 31, 2005
62
7
Sweden
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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
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:
Code:
<?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

Moderator emeritus
Jun 15, 2000
7,958
7
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:
Code:
- (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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.