2012-12-28 69 views
0

我在支持文件中有一個名爲「cards」的文件夾。我正在嘗試將pic設置爲對象的UIImage實例變量。當我嘗試NSLog的UIImage我得到空。將圖片設置爲UIImages作爲實例變量

我不認爲我正確訪問圖片的路徑,但不知道?

#import "Deck.h" 
#import "Card.h" 

@implementation Deck 

@synthesize cards; 

- (id) init 
{ 
    if(self = [super init]) 
    { 
     cards = [[NSMutableArray alloc] init]; 

     NSInteger aCount, picNum = 0; 

     for(int suit = 0; suit < 4; suit++) 
     { 
      for(int face = 1; face < 14; face++, picNum++) 
      { 

       //NSString *path = [[NSBundle mainBundle] bundlePath]; 
       //NSString *imagePath = [path stringByAppendingPathComponent: [NSString stringWithFormat:@"/cards/card_%d.png",picNum]]; 

       NSString *fileName = [NSString stringWithFormat:@"card_%d", picNum]; 
       NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"png"inDirectory:@"/cards"]; 

       NSLog(@"%@", path); 
       UIImage *output = [UIImage imageNamed:path]; 

       //NSLog(@"%@", output); 

       Card *card = [[Card alloc] initWithFaceValue:(NSInteger)face 
               countValue:(NSInteger)aCount 
               suit:(Suit)suit 
               cardImage:(UIImage *)output]; 

       [cards addObject:card]; 
      } 

     } 
    } 
    return self; 
} 

} 

@end

+0

你爲什麼要鑄造的'每一個參數 - [Card init ...]'? – 2012-12-28 16:03:39

+0

它在一個循環初始化52卡對象,然後將它們添加到一個數組,我只是沒有發佈所有的代碼 – blitzeus

回答

1

的概念更好的方法是使用那些專門爲子目錄的工作設計的NSBundle方法:

NSString *fileName = [NSString stringWithFormat:@"card_%d", picNum]; 
NSString *path = [[NSBundle mainBundle] pathForResource:fileName 
               ofType:@"png" 
              inDirectory:@"cards"]; 
+0

這仍然輸出空當我NSLog的UIImage – blitzeus

+0

文件實際存在於文件系統?他們有效的圖像? – 2012-12-28 17:29:17

+0

我可以通過IB選擇一個文件並顯示它,但我想嚴格按照代碼 – blitzeus