2010-01-14 126 views
0

我們有這樣iPhone圖像內存泄露

NSData* imageData; 
UIImage* imageForData; 
UIImageView* imageView; 

NSData* imageData; 
UIImage* imageForData; 
UIImageView* imageView; 
    CellWithId * cell = [[CellWithId alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]; 

CGRect frame; 
frame.origin.x = 5; 
frame.origin.y = 0; 
frame.size.height = 43; 
frame.size.width = 52; 

if ([[planDictionary objectAtIndex:indexPath.row] objectForKey:@"url"]!=[NSNull null]) { 
    imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[[planDictionary objectAtIndex:indexPath.row] objectForKey:@"url"]]]; 
    imageForData = [[UIImage alloc] initWithData:imageData]; 
    imageView = [[UIImageView alloc] initWithImage:imageForData]; 
    imageView.frame = frame; 
    [cell.contentView addSubview:imageView]; 
    [imageData release]; 
    [imageForData release]; 

}NSData* imageData; 
UIImage* imageForData; 
UIImageView* imageView; 
//CellWithId *cell = (CellWithId*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
// if (cell == nil) { 
CellWithId * cell = [[CellWithId alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]; 
//} 

CGRect frame; 
frame.origin.x = 5; 
frame.origin.y = 0; 
frame.size.height = 43; 
frame.size.width = 52; 

if ([[planDictionary objectAtIndex:indexPath.row] objectForKey:@"url"]!=[NSNull null]) { 
    imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[[planDictionary objectAtIndex:indexPath.row] objectForKey:@"url"]]]; 
    imageForData = [[UIImage alloc] initWithData:imageData]; 
    imageView = [[UIImageView alloc] initWithImage:imageForData]; 
    imageView.frame = frame; 
    [cell.contentView addSubview:imageView]; 
    [imageData release]; 
    [imageForData release]; 

}else { 
    //imageForData = [UIImage imageNamed:@"Plans-Default-image.jpg"]; 
    imageForData = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Plans-Default-image" ofType:@"jpg"]]; 

    imageView = [[UIImageView alloc] initWithImage:imageForData]; 
    imageView.frame = frame; 
    [cell.contentView addSubview:imageView]; 
} 

[imageView release]; 

我不知道什麼是問題的一個代碼,但爲imageData = [[NSData的頁頭] initWithContentsOfURL:[NSURL URLWithString:[planDictionary objectAtIndex:indexPath.row] objectForKey:// 「網址」]]];是一個總是顯示內存泄漏的地方。請幫我調試這個問題。

+0

它說什麼對象泄露? – coneybeare 2010-01-14 14:14:32

+0

你粘貼了兩次代碼嗎?或者你的代碼實際上看起來如何? – FelixLam 2010-01-14 14:30:31

回答

1

您分配兩個CellWithId並且從不釋放它。它看起來好像你沒有正確實施這種方法。細胞在

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

應自動釋放回:

CellWithId * cell = [[[CellWithId alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
// ... 
return (UITableViewCell *)cell; 

你也應該使用正確dequeueReusableCellWithIdentifier:CellIdentifier有不俗的表現。