UITableView使用imageWithContentsOfFile存在内存泄漏问题

3
我在我的标准图像/文本视图的UITableView中出现了内存泄漏。我使用imageWithContentsOfFile加载图像,并将其分配给我cellimageView.image
这个图像不会自动释放,我必须在-dealloc方法中遍历单元格并将其设置为0。
但每次显示UITableView时,我仍然会失去内存,我真的不知道为什么。我还删除了cell中的autorelease并尝试手动释放它,但我仍然失去内存。
Alex
- (id) initWithProfils:(ColorPainterProfiles*) profils listener:(NSObject<ColorPainterTableListener>*) listener{
    self = [super init];
    if (self) {
        _listener=listener;
        _profils=profils;        
    }
    return self;
}

- (void)dealloc
{
    for (int section = 0; section < [self.tableView numberOfSections]; section++) {
        for (int row = 0; row < [self.tableView numberOfRowsInSection:section]; row++) {
        NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:section];
        UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:cellPath];
        cell.imageView.image=0;
        //[cell release];
        }
    }

    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}


// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{

    //((UILabel*)self.navigationItem.titleView).text=NSLocalizedString(@"Change Picture", @""); 

}

- (void) setTableView:(UITableView *)tableView {
    [super setTableView:tableView];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    if ([LibTools isIPad]) {
        self.tableView.rowHeight=self.tableView.rowHeight*3;
    } else {
        self.tableView.rowHeight=self.tableView.rowHeight*2;
    }

}

- (void)viewDidUnload
{
    [super viewDidUnload];
}



- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // delete your data item here
        [_profils deleteAt:[_profils.profils objectAtIndex:indexPath.row]];
        [_profils removeProfileAt:indexPath.row];
        // Animate the deletion from the table.
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [_listener actionColorPainterTable:[_profils.profils objectAtIndex:indexPath.row] sender:self];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // There is only one section.
    return 1;
}

- (NSInteger) numberOfRows {
           return _profils.profils.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self numberOfRows];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"ManageModels";

    // Try to retrieve from the table view a now-unused cell with the given identifier.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    // If no cell is available, create a new one using the given identifier.
    if (cell == nil) {
        // Use the default cell style.
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    } else {
        cell.imageView.image=0;
    }

    ColorPainterProfile *cpp=[_profils.profils objectAtIndex:indexPath.row];

    cell.textLabel.text=cpp.title;

    NSString *path=[LibTools documentsSubFolder:cpp._subFolder];
    cell.imageView.image =[UIImage imageWithContentsOfFile:[path stringByAppendingPathComponent:cpp._imageName]];
    //cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;



    // Set up the cell.
    //cell.textLabel.textColor=[UIColor grayColor];

    return cell;
}

/*- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)sec {
    return @"Hello";
}*/

/*- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}*/


- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

问题不会出现在您的dealloc中,您不应该在那里编写代码。请问您能否发布您的cellForRowAtIndexPath:方法或任何其他设置图像的方法? - KDaker
2个回答

0
谢谢大家,我找到了前一个视图中的泄漏问题,现在一切都正常工作,dealloc 中没有任何代码...

0

如果出现内存泄漏,可能的原因是与数组_profils.profils有关。或者您可以像分配和释放图像一样操作,

 NSString *path=[LibTools documentsSubFolder:cpp._subFolder];
 UIImage *cellImage = [[UIImage alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:cpp._imageName]];
 cell.imageView.image =cellImage;
 [cellImage release];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接