2010-09-15 83 views
2

我一直拉我的頭髮在這個問題上,我已閱讀所有關於釋放問題的帖子,但不明白爲什麼,因爲我很新的Objective-C和iPhone,你可以告訴: )UIImage釋放問題

但我不明白的是代碼下面說我已經過度釋放UIImage的,現在我已經嘗試了一切,但應用程序仍然崩潰

UIImage *imageSave  = [UIImage imageNamed:@"btn_save.png"]; 
UIButton *btnSave  = [UIButton buttonWithType:UIButtonTypeCustom]; 

[btnSave setBackgroundImage:imageSave forState:UIControlStateNormal]; 

btnSave.frame = CGRectMake(0, 0, imageSave.size.width, imageSave.size.height); 

[btnSave addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside]; 

UIBarButtonItem *barBtnSave = [[UIBarButtonItem alloc] initWithCustomView:btnSave]; 

self.navigationItem.rightBarButtonItem = barBtnSave; 

//[imageSave release]; 
//[btnSave release]; 
[barBtnSave release]; 


UIImage *imageCancel = [UIImage imageNamed:@"btn_cancel.png"]; 
UIButton *btnCancel  = [UIButton buttonWithType:UIButtonTypeCustom]; 

[btnCancel setBackgroundImage:imageCancel forState:UIControlStateNormal]; 

btnCancel.frame = CGRectMake(0, 0, imageCancel.size.width, imageCancel.size.height); 

[btnCancel addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside]; 
UIBarButtonItem *barBtnCancel = [[UIBarButtonItem alloc] initWithCustomView:btnCancel]; 

self.navigationItem.leftBarButtonItem = barBtnCancel; 

//[imageCancel release]; 
//[btnCancel release]; 
[barBtnCancel release]; 
+0

即使註釋掉調用「釋放」你有一個問題?還是你想了解爲什麼你必須評論他們? – imaginaryboy 2010-09-15 05:37:57

回答

1

確定該塊,讓我們來看看這些2行:

UIImage *imageCancel = [UIImage imageNamed:@"btn_cancel.png"]; 

[imageCancel release]; 

在1號線,在創建自動釋放圖像,那麼retainCount將是0。然後,鬆開圖像,這將使retainCount爲-1​​,導致崩潰

你需要的是要記住增加你的retainCount的列表:alloccopyretain ...如果你調用這些方法之一的一個對象,你要做的之一:autoreleaserelease

+0

謝謝你會嘗試 – badcoder 2010-09-15 15:27:32