0

画像が単語のアナグラムを思いつくアプリを作ろうとしています。アナグラムの部分はありますが、画像が表示されません。写真は「images」というフォルダに保存されます。anagramPics - item 0 と同時に anagrams -item 0 を呼び出して、単語と pic を一致させたいと思います。

前もって感謝します。

anagramPics - item 0 と同時に anagrams -item 0 を呼び出して、単語と 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

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

1 に答える 1

0

表示する固定画像がいくつかある同様の状況があります。これが私がしたことです。

ストーリーボードに UIImageView を追加しました。制約などを使用して、必要な場所に配置しました。

そのための IBOutlet を作成しました。それを myImageView と呼びます。

すべての画像をアセットとして追加しました。私の場合、さまざまなクレジット カードの画像を表示していますが、原理は同じです。

Images.xcassets をクリックします。

クレジットカードの画像アセット

Xcodeではこんな感じ

各画像アセットは、Contents.json ファイルとそれに付随する画像ファイルを含むフォルダーです。3 つのファイルがあることに注意してください。iOS は Retina、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"
    }
}

アセット フォルダ

コードで、image プロパティを設定して UIViewImage の画像を変更します。

(これは Swift コードです。自分で Objective-C に変換する必要があります。)

myImageView.image = UIImage(named:"Card VISA")
于 2016-03-19T20:58:01.640 に答える