gpt4 book ai didi

xcode - 将图像路径存储在plist中然后检索以在屏幕上显示

转载 作者:行者123 更新时间:2023-11-30 13:35:39 25 4
gpt4 key购买 nike

我正在尝试制作一个应用程序,其中图片会显示单词的字谜。我有字谜部分,但我无法显示图片。图片保存在名为“images”的文件夹中。我想调用 anagramPics - item 0,同时调用 anagrams -item 0 - 将单词与图片匹配

提前致谢。

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

字谜代码:

级别.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

级别.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

游戏 Controller .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);
}

最佳答案

我也有类似的情况,我有一些固定的图像要显示。这就是我所做的。

在 Storyboard 上添加了 UIImageView。使用约束等将其放置在我想要的位置。

为其创建了一个 IBOutlet。将其命名为 myImageView。

我将所有图像添加为 Assets 。就我而言,我显示了各种信用卡图像,但原理是相同的。

Click on Images.xcassets

Credit card image assets

Looks like this in Xcode

每个图像资源都是一个文件夹,其中包含 Contents.json 文件及其附带的图像文件。您会注意到有三个文件。 iOS 将使用适合视网膜、iPhone 6plus 等的正确尺寸图像。

Contents.json 看起来像这样:

{
"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"
}
}

asset folder

在代码中,通过设置 UIViewImage 的 image 属性来更改其图像。

(这是 Swift 代码。您必须自己将其转换为 Objective-C。)

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

关于xcode - 将图像路径存储在plist中然后检索以在屏幕上显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36104512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com