【问题标题】:iPhone image memory leakiPhone图像内存泄漏
【发布时间】:2010-01-14 13:31:54
【问题描述】:

我们有这样的代码

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"]]];是一个总是显示内存泄漏的地方。请帮我调试这个问题。

【问题讨论】:

  • 它说什么对象被泄露了?
  • 您是否粘贴了两次代码,还是您的代码实际上是这样的?

标签: iphone image memory-leaks


【解决方案1】:

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

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

应该自动释放:

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

您还应该使用dequeueReusableCellWithIdentifier:CellIdentifierproperly 以获得良好的性能。

【讨论】:

    猜你喜欢
    • 2010-11-23
    • 2011-12-10
    • 2011-05-24
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多