Утечка памяти в изображении iPhone - PullRequest
0 голосов
/ 14 января 2010

У нас есть такой код

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 Ответ

1 голос
/ 14 января 2010

Вы выделяете два CellWithId и никогда не отпускаете его. Похоже, что вы не правильно реализуете этот метод. Ячейка вернулась в

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

должен быть автоматически выпущен:

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

Также вы должны правильно использовать dequeueReusableCellWithIdentifier:CellIdentifier, чтобы иметь приличную производительность.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...