简体   繁体   中英

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.

我想调用anagramPics-项目0,同时将anagrams -item 0-匹配单词和图片

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);    
}

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.

单击Images.xcassets

信用卡图片资产

在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" : "card-visa@1x.png"
        },
        {
            "idiom" : "universal",
            "scale" : "2x",
            "filename" : "card-visa@2x.png"
        },
        {
            "idiom" : "universal",
            "scale" : "3x",
            "filename" : "card-visa@3x.png"
        }
    ],
    "info" : {
        "version" : 1,
        "author" : "xcode"
    }
}

资产文件夹

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM