iPhone图像内存泄漏

时间:2010-01-14 13:31:54

标签: iphone image memory-leaks

我们有这样的代码

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 alloc] initWithContentsOfURL:[NSURL URLWithString:[[planDictionary objectAtIndex:indexPath.row] objectForKey:@“url”]]];是一个总是显示内存泄漏的地方。请帮我调试这个问题。

1 个答案:

答案 0 :(得分:1)

您分配了两个CellWithId并且永远不会释放它。看起来好像没有正确实现此方法。

中的单元格返回
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

应自动释放:

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

此外,您应该正确使用dequeueReusableCellWithIdentifier:CellIdentifier以获得不错的表现。