Search code examples
xcodeswiftplistproperty-list

Storing image path in plist then retrieve to show on screen


I am trying to make an app where the picture comes up with the anagram of a word. I have the anagram part but I can't get the picture to come up. The picture is saved in a folder called "images". I would like to call anagramPics - item 0, at the same time as anagrams -item 0 - to match the word with the pic

Thanks in advance.

I would like to call anagramPics - item 0, at the same time as anagrams -item 0 - to match the word with the pic

code for the anagram:

level.m

#import "Level.h"

@implementation Level

+(instancetype)levelWithNum:(int)levelNum;
{
 // find .plist file for this level
  NSString* fileName = [NSString stringWithFormat:@"level%i.plist", levelNum];
  NSString* levelPath = [[[NSBundle mainBundle] resourcePath]      stringByAppendingPathComponent:fileName];

  // load .plist file
  NSDictionary* levelDict = [NSDictionary dictionaryWithContentsOfFile:levelPath];

  // validation
  NSAssert(levelDict, @"level config file not found");

  // create Level instance
  Level* l = [[Level alloc] init];

  // initialize the object from the dictionary
  l.pointsPerTile = [levelDict[@"pointsPerTile"] intValue];
  l.anagrams = levelDict[@"anagrams"];
  l.timeToSolve = [levelDict[@"timeToSolve"] intValue];

  return l;
}

@end

level.h

#import <Foundation/Foundation.h>

@interface Level : NSObject

//properties stored in a .plist file
@property (assign, nonatomic) int pointsPerTile;
@property (assign, nonatomic) int timeToSolve;
@property (strong, nonatomic) NSArray* anagrams;

//factory method to load a .plist file and initialize the model
+(instancetype)levelWithNum:(int)levelNum;

@end

gameController.h

-(void)dealRandomAnagram
{
//1
NSAssert(self.level.anagrams, @"no level loaded");

//2 random anagram
int randomIndex = arc4random()%[self.level.anagrams count];
NSArray* anaPair = self.level.anagrams[ randomIndex ];

//3
NSString* anagram1 = anaPair[0];
NSString* anagram2 = anaPair[1];

//4
int ana1len = [anagram1 length];
int ana2len = [anagram2 length];

//5
NSLog(@"phrase1[%i]: %@", ana1len, anagram1);
NSLog(@"phrase2[%i]: %@", ana2len, anagram2);    
}

Solution

  • I have a similar situation where I have some fixed images to display. This is what I did.

    Added a UIImageView on Storyboard. Positioned it out where I wanted it using constraints etc.

    Created an IBOutlet for it. Call it myImageView.

    I added all my images as assets. In my case I'm displaying various credit card images but the principle is the same.

    Click on Images.xcassets

    Credit card image assets

    Looks like this in Xcode

    Each image asset is a folder with a Contents.json file and the image files that go with it. You will note that there are three files. iOS will use the correct size image for retina, iPhone 6plus etc.

    Contents.json looks like this:

    {
        "images" : [
            {
                "idiom" : "universal",
                "scale" : "1x",
                "filename" : "[email protected]"
            },
            {
                "idiom" : "universal",
                "scale" : "2x",
                "filename" : "[email protected]"
            },
            {
                "idiom" : "universal",
                "scale" : "3x",
                "filename" : "[email protected]"
            }
        ],
        "info" : {
            "version" : 1,
            "author" : "xcode"
        }
    }
    

    asset folder

    In the code, change the image of the UIViewImage by setting its image property.

    (This is Swift code. You will have to convert it to Objective-C yourself.)

    myImageView.image = UIImage(named:"Card VISA")