1

I know this is probably a silly question but I'm storing most of my game data in a plist - with that I'd like to include references to images used within my game - same hierarchal level as 'supporting files'. I have different types of images stored in 3 separate folders. One folder for example is called imageclue. How could I store the path in my plist, I'm stuck because I can't just store the path in my plist as string - filename.jpg. I've tried getting the path of the file but when I log it out it .

Sorry if I'm not explaining well and thank you in advance for any help :)

EDIT**

I have a plist file added to my program I don't want to programatically add to it as the images are constants - the screenshots below show a tutorial instead of the filename.jpg (because that won't work seen as my images are stored in a file) I wondered what path name do I use as a string.

enter image description here

The image is from a tutorial off of appcoda.com - where it says thumbnails are the image path files. If you look at where the images are stored on the left - they are stored with the program files. My images are in a folder in there so I'm confused as to what to enter in my plist for the image file.

Hope this clears up what I meant, sorry :)

  • It doesn't look like you finished the question. How do you want to use the images? You need a list of the images in each folder? – Wain Apr 25 '13 at 19:02
  • Hi @Maria i have posted answer to read images with your plist data. Good Luck, and let me know insights about it. – Dipen Panchasara Apr 25 '13 at 20:12

3 Answers3

0

Do it like this,

NSDictionary *imagePaths = @{@"image 1": [NSHomeDirectory() stringByAppendingPathComponent:@"image 1"]};
[self writeToPlist:imagePaths];

- (void)writeToPlist:imagePaths:(id)plist{
  NSError *error;
  NSData *data = [NSPropertyListSerialization dataWithPropertyList:plist format:kCFPropertyListXMLFormat_v1_0 options:0 error:&error];
  if(error){
    NSLog(@"Could not write to file");
    return;
  }
  [data writeToFile:[self plistPath] atomically:YES];
}

Like wise loading is simple as this;

[self loadImagePathForImageNamed:@"image 1"];
- (NSString*)loadImagePathForImageNamed:(NSString*)imageName{

}


- (NSString*)loadImagePathForImageNamed:(NSString*)imageName{
    NSData *data = [NSData dataWithContentsOfFile:[self plistPath]];
  NSString *error;
  NSPropertyListFormat format;
  NSDictionary *dictionary = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];
  if(error){
    NSLog(@"Could not open plist %@", error);
    return nil;
  }
  return dictionary[imageName];
}

You may have to handle the error when the file is not there by creating a new one, otherwise this should work.

Sandeep
  • 20,908
  • 7
  • 66
  • 106
0

Store three variables in .h file

@interface YourViewController : UIViewController 
{
    NSString *folder1;
    NSString *folder2;
    NSString *folder3;
}

in viewdidload:

-(void) viewdidLoad
{
    //get the documents directory:
    NSArray *paths = NSSearchPathForDirectoriesInDomains
    (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    //getting the folder name:
    folder1 = [NSString stringWithFormat:@"%@/imageclue",
                           documentsDirectory];

    folder2 = [NSString stringWithFormat:@"%@/folder2",
                           documentsDirectory];

    folder3 = [NSString stringWithFormat:@"%@/folder3",
                           documentsDirectory];

}

-(NSArray*) getPlistFromFolder:(NSString*)folder imageName:(NSString*)image
{
    NSString *imageTitle = [NSString stringWithFormat:@"%@/image",
                       folder];

    NSArray *data = [[NSArray alloc] initWithContentsOfFile:plistName];
    return data;
}

So in the plist file, just store the image name.

Hope this helps...

Muhammad Adnan
  • 2,668
  • 15
  • 27
lakshmen
  • 28,346
  • 66
  • 178
  • 276
  • I don't understand the NSArray method? but thank you for the help on the header variables:). –  Apr 26 '13 at 10:53
  • sorry.. my mistake... that method gets data from the plist file... can change it according to your situation.. – lakshmen Apr 26 '13 at 11:05
  • I wouldn't need to write that method if I was loading data from the plist elsewhere? For example here? +(instancetype)levelWithNum:(int)levelNum { NSString* fileName = [NSString stringWithFormat:@"EachLevel%i.plist", levelNum]; NSString* levelPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName]; NSDictionary* levelDict = [NSDictionary dictionaryWithContentsOfFile:levelPath]; NSAssert(levelDict, @"level config file not found"); LoadPlistLevel* lev = [[LoadPlistLevel alloc] init]; lev.everylevel = levelDict[@"everylevel"]; return lev;} –  Apr 26 '13 at 12:33
  • that function is ok. after getting the levelDict, get the value using the key u pass. – lakshmen Apr 26 '13 at 13:49
  • how would I do that? I added this to viewDidLoad just to check - but the image doesn't show up. `code`UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@/4[1].jpg",folder1]]]; [self.view addSubview:imageView];`code` –  Apr 26 '13 at 14:01
0

You are storing path right way, just need to store filename of image with extension in plist when your images are in your Application Bundle, for more reference you can define key name Instead "item1", "item2" in your plist.

Now coming to actual Question, how to access image from plist

Step 1 : Read your recipes.plist from Application Bundle

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"recipes" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:bundlePath];

Step 2 : Now Get Image/Thumbnails name out of it, which you want to load

Step 3 : Define following Function in your Controller, which returns image from name

- (UIImage *)getImageWithName:(NSString *)imageFileName
{
    NSString *ext = [imageFileName pathExtension];
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:[imageFileName stringByDeletingPathExtension] ofType:ext];
    return [UIImage imageWithContentsOfFile:imagePath];
}

HOW TO USE

Suppose you want to load Image with key "Item2" then write following code

NSString *imageFileName = [[dict objectForKey:@"Thumbnail"] valueForKey:@"Item2"];
UIImage *item2Image = [self getImageWithName:imageFileName];

For "Item6"

NSString *imageFileName1 = [[dict objectForKey:@"Thumbnail"] valueForKey:@"Item6"];
UIImage *item6Image = [self getImageWithName:imageFileName1];
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57