开发者

iPhone image memory leak

开发者 https://www.devze.com 2022-12-16 04:18 出处:网络
We have a code like this NSData* imageData; UIImage* imageForData; UIImageView* imageView; NSData* imageData;

We have a code like this

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];

I dont know what is the problem but imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[[planDictionary objectAtIndex:indexPath.row] objectForKey:@"url"]]]; is a place which always shows memory leaks. Please help me to debug this issue.


You allocate two CellWithIdand never release it. It looks as though you do not properly implement this method. The cell returned in

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

should be autoreleased:

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

Also you should use dequeueReusableCellWithIdentifier:CellIdentifierproperly to have decent performance.

0

精彩评论

暂无评论...
验证码 换一张
取 消