釋放圖像時,我會通過對iPhone表視圖的教程。以下是填充表格的數據源方法。崩潰中的UITableViewCell
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
static NSString* SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
UIImage* image = [UIImage imageNamed:@"face.png"];
cell.imageView.image = image;
//[image release];
return cell;
}
我注意到作者在將它設置爲表格單元格的圖標後沒有釋放圖像。源代碼定義UITableCell.imageView.image
確實,事實上,表明它是一個保留的屬性。如果我不釋放自己的圖像副本,則在方法結束時保留計數最終爲2。所以我決定釋放它來釋放內存。但是,這樣做會導致iPhone模擬器在我將桌面略微滑離屏幕時崩潰。爲什麼?
假設每當我構造一個沒有'alloc'的對象時,它是一個自動釋放對象嗎? – JoJo 2011-04-17 03:17:57
@JoJo - 偉大的問題 - 請參閱我的編輯。 – Moshe 2011-04-17 03:19:58