2016-03-19 47 views
0

我正在試圖製作一個應用程序,在該應用程序中,圖片中出現了一個單詞的字謎。我有anagram的一部分,但我不能讓圖片出現。照片保存在名爲「圖像」的文件夾中。我想打電話給anagramPics - 項目0,同時作爲anagrams -item 0 - 以匹配單詞與圖片在plist中存儲圖像路徑,然後檢索以顯示在屏幕上

在此先感謝。對於字謎

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

代碼:

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

請發表您的代碼。 – ryantxr

+0

我沒有看到任何試圖顯示圖像的代碼。 – ryantxr

+0

這就是我遇到的問題。我不知道該怎麼稱呼它。 – isabella

回答

0

我有類似的情況,我有一些固定的圖像顯示。這就是我所做的。

在Storyboard上添加了UIImageView。將其定位在使用約束條件等我想要的地方。

爲它創建了一個IBOutlet。稱之爲myImageView。

我將所有圖像添加爲資產。在我的情況下,我顯示各種信用卡圖像,但原則是一樣的。

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" : "[email protected]" 
     }, 
     { 
      "idiom" : "universal", 
      "scale" : "2x", 
      "filename" : "[email protected]" 
     }, 
     { 
      "idiom" : "universal", 
      "scale" : "3x", 
      "filename" : "[email protected]" 
     } 
    ], 
    "info" : { 
     "version" : 1, 
     "author" : "xcode" 
    } 
} 

asset folder

在代碼中,通過設置改變UIViewImage的圖像的圖像屬性。

(這是SWIFT CODE。你必須把它轉換成Objective-C的自己。)

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

您究竟在哪裏放置了您的代碼?這實際上是個好主意!所以謝謝! – isabella

+0

在我的情況下,它是在tableview中,所以它在tableview:cellForRowAtIndexPath。我不能告訴你該把它放在哪裏,因爲這取決於你的應用程序的結構。如果您使用按鈕更改圖像,請將其放入該按鈕的IBAction中。 – ryantxr

相關問題