2013-08-05 213 views
2

在我的拆分視圖iPad應用程序中,默認的詳細視圖從數組中隨機加載圖像,並在用戶返回該視圖時執行此操作。該應用程序加載罰款與該視圖,我可以去另一個視圖罰款。問題是,如果我回到那個視圖,有時它會崩潰,有時它會崩潰,如果我回到默認視圖後選擇另一個視圖。當我運行泄漏工具時,我沒有顯示任何泄漏,並且每次發生崩潰時都不會在日誌中顯示任何內容。我確實收到了「接收到的內存警告」記錄,所以它的崩潰必定與某處泄漏有關,我只是不確定在哪裏。我正在使用ARC。有任何想法嗎?收到內存警告iOS

這裏是我的viewDidLoad方法:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    UIImage *agelity = [UIImage imageNamed:@"Agelity"]; 
    UIImage *agelity2 = [UIImage imageNamed:@"Agelity2"]; 
    UIImage *biltmore = [UIImage imageNamed:@"Biltmore"]; 
    UIImage *biltmore2 = [UIImage imageNamed:@"Biltmore2"]; 
    UIImage *biltmore3 = [UIImage imageNamed:@"Biltmore3"]; 
    UIImage *choice = [UIImage imageNamed:@"Choice"]; 
    UIImage *enterprise = [UIImage imageNamed:@"Enterprise"]; 
    UIImage *enterprise2 = [UIImage imageNamed:@"Enterprise2"]; 
    UIImage *grainger = [UIImage imageNamed:@"Grainger"]; 
    UIImage *grainger2 = [UIImage imageNamed:@"Grainger2"]; 
    UIImage *greatWolf = [UIImage imageNamed:@"Great_Wolf"]; 
    UIImage *greatWolf2 = [UIImage imageNamed:@"Great_Wolf2"]; 
    UIImage *officeDepot = [UIImage imageNamed:@"Office_Depot1"]; 
    UIImage *officeDepot2 = [UIImage imageNamed:@"Office_Depot2"]; 
    UIImage *officeDepot3 = [UIImage imageNamed:@"Office_Depot3"]; 
    UIImage *sams = [UIImage imageNamed:@"Sams"]; 
    UIImage *sams2 = [UIImage imageNamed:@"Sams2"]; 

    NSMutableArray *benefitAds = [[NSMutableArray alloc]initWithObjects:agelity, agelity2, biltmore, biltmore2, biltmore3, choice, enterprise, enterprise2, grainger, grainger2, greatWolf, greatWolf2, officeDepot, officeDepot2, officeDepot3, sams, sams2, nil]; 

    int randomIndex = arc4random() % [benefitAds count]; 

    adImage.image = [benefitAds objectAtIndex:randomIndex]; 

    [self configureView]; 
} 

編輯:我試圖用使用imageWithData代替imageNamed的建議,所以我這樣做:

NSData *agelityData = [NSData dataWithContentsOfFile:@"Agelity"]; 
    UIImage *agelity = [UIImage imageWithData:agelityData]; 

但現在的應用與上線發生衝突:

int randomIndex = arc4random() % [benefitAds count]; 

與:

Thread 1: EXC_ARITHMETIC(code=EXC_I386_DIV, subcode=0x0) 

當我在我的設備,而不是在模擬器上運行它,我得到這個:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 3051310543 beyond bounds for empty array' 

編輯:我設置異常斷點,因爲我得到一個EXC_BAD_ACCESS碼= 1個錯誤。看起來應用程序在我改變細節視圖時會隨機崩潰。我想我會創建一個新的問題。

感謝您的幫助!

+1

順便說一句,以避免模偏置,而不是'與''%結合運營商arc4random',您可能需要使用['arc4random_uniform'(http://developer.apple。 COM /庫/ MAC /文檔/達爾文/參考/手冊頁/ man3/arc4random_uniform.3.html)。但盧卡斯的回答應該解決記憶問題。 – Rob

+0

好的,現在只需要使用imageNamed,你的文件路徑沒有被正確使用,這是一組完全不同的iOS細節...無論如何,與陣列人的建議一起去吧。我不認爲你正在釋放該陣列。 –

回答

5

我不知道這是否會導致崩潰(可能性很高),但我真的建議您不要將所有圖像存儲在陣列中。

更好的方法是存儲圖像的名稱,並分配一個UIImage,並選擇名稱。

看到這個:

- (void)viewDidLoad 
{ 
    NSMutableArray *benefitAds = [[NSMutableArray alloc]initWithObjects:@"Agelity", @"Agelity2", @"Biltmore", @"Biltmore2", @"Biltmore3", @"Choice", @"Enterprise", @"Enterprise2", @"Grainger", @"Grainger2", @"Great_Wolf", @"Great_Wolf2", @"Office_Depot1", @"Office_Depot2", @"Office_Depot3", @"Sams", @"Sams2", nil]; 

    int randomIndex = arc4random() % [benefitAds count]; 

    if(randomIndex < [benefitAds count]) { 
     adImage.image = [UIImage imageNamed:[benefitAds objectAtIndex:randomIndex]]; 
     [self configureView]; 
    } 
    else 
    { 
    //error message 
    } 
} 

請給一些反饋它的工作與否。

編輯:

嘗試檢查隨機數得到的是使用它之前,真正有效的索引。

+0

這沒有奏效。 – raginggoat

+2

@ user2029585這絕對是正確的做法。您應該只爲在任何給定點可見的圖像創建'UIImage'對象。當你擁有'UIImage'對象的數組時,你可以很容易地遇到內存問題。 – Rob

+0

再試一次,@ user2029585。如果那仍然不起作用,肯定問題在別的地方。 –

3

imageNamed:使用不會在內存警告中自行清空的內部緩存。嘗試imageWithData

+0

查看我的問題的更新。 – raginggoat

+1

技術上,是的,但從概念上講,我不確定是否正確。是的,它不響應內存警告,但它確實響應內存壓力。 ('NSCache'表現出類似的行爲。) – Rob

+0

我使用來自Web服務的imageData。問題依然存在。 –

1
- (void)didReceiveMemoryWarning { 

    if([self isViewLoaded] && self.view.window == nil) { 
     self.view = nil; 
    } 

    [super didReceiveMemoryWarning]; 
} 
+0

謝謝解決了我的問題...... :) :) :) –