2012-02-06 16 views
0

我已經加載了一個數組顯示數組內容:用的NSString

currentBackground=4; 
bgImages = [[NSArray alloc] initWithObjects: 
      [UIImage imageNamed:@"mystery_01 320x460"], 
      [UIImage imageNamed:@"mystery_02 320x460"], 
      [UIImage imageNamed:@"mystery_03 320x460"], 
      [UIImage imageNamed:@"mystery_04 320x460"], 
      [UIImage imageNamed:@"mystery_05 320x460"], 
      nil]; 

現在我想在當前正在顯示的圖像的文件名的標籤顯示。我想:

mainLabel.text = [NSString stringWithFormat:@"bgImages= %@",[bgImages objectAtIndex:currentBackground]]; 

會工作,但我得到的是十六進制代碼。我有一個很好地滾動圖像的按鈕。但是,當我嘗試顯示圖像名稱時,我所得到的就是我認爲是名稱所在的地址。

回答

2

圖像的文件名不存儲在您的UIImage對象中。 UIImage類中沒有這樣的屬性。你必須自己儲存名字。也許你可以存儲在文件名中的數組,並且使用這個數組既檢索文件中的圖像,再後來來查找圖像文件名:

imageFileNames = [[NSArray alloc] initWithObjects: 
          @"mystery_01 320x460", 
          @"mystery_02 320x460", 
          @"mystery_03 320x460", 
          @"mystery_04 320x460", 
          @"mystery_05 320x460", 
          nil]; 

檢索圖像(你可以使用一個換在循環的這個代替,如果你願意的話):

bgImages = [[NSArray alloc] initWithObjects: 
      [UIImage imageNamed:[imageFileNames objectAtIndex:0]], 
      [UIImage imageNamed:[imageFileNames objectAtIndex:1]], 
      [UIImage imageNamed:[imageFileNames objectAtIndex:2]], 
      [UIImage imageNamed:[imageFileNames objectAtIndex:3]], 
      [UIImage imageNamed:[imageFileNames objectAtIndex:4]], 
      nil]; 

從文件名陣列獲取文件名:

mainLabel.text = [NSString stringWithFormat:@"bgImages= %@",[imageFileNames objectAtIndex:currentBackground]]; 
0

在你的字符串格式有效使用%@只是調用對象的方法description。您看到的十六進制值只是對象指針的值 - 這是NSObject的description方法的默認實現。

請注意,對於NSString對象,description方法返回...您猜對了 - NSString對象本身。

在你的情況下,要顯示圖像名稱,你必須保留它們,因爲它似乎UIImage的description不會返回圖像名稱。

您還可以製作一個UIImage子類,它記住名稱,但不確定它是否值得麻煩。