在segue后退按钮后,UITableViewCell的UIImage内存未被释放

时间:2014-08-09 07:09:09

标签: ios objective-c uitableview uiimageview xcode6

在我的应用程序中,我在每个单元格中加载了一些本地图像文件,并在表视图控制器中加UIImageView。但问题是当按下导航栏中的后退按钮时,没有释放为图像视图分配的内存并导致内存泄漏:

这是内存日志,因为我来回查看我的表View: enter image description here

这是导致我正在谈论的表视图控制器的prepareForSegue

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.

    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];

    if (indexPath) {
        if (indexPath.section == 0) {
            //a section is selected

            if ([segue.destinationViewController isKindOfClass:[SectionTableViewController class]]) {
                 NSString *jsonFilePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"s%ld", (long)indexPath.row + 1] ofType:@"json"];
                 NSData *data = [NSData dataWithContentsOfFile:jsonFilePath];
                 NSDictionary *sectionData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
                 SectionTableViewController *stvc = (SectionTableViewController *)segue.destinationViewController;
                 stvc.firstPrb = [sectionData objectForKey:@"firstPrb"];
                 stvc.lastPrb = [sectionData objectForKey:@"lastPrb"];
                 stvc.sectionTitle = [sectionData objectForKey:@"sectionTitle"];
            }
        }
    }
    }

这是用于在上述表视图控制器中生成单元格的代码:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ProverbCell" forIndexPath:indexPath];

    // Configure the cell...
    NSString *jsonFilePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%ld", self.firstProverb.intValue + indexPath.row] ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:jsonFilePath];
    NSDictionary *proverbData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

    cell.textLabel.text = [proverbData objectForKey:@"proverb"];
    @autoreleasepool {
    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg", self.firstProverb.intValue + indexPath.row]];
    }

    return cell;
    }

现在的问题是:点击segue后退按钮后不应释放内存吗?!没有强烈的指向图像视图或其他任何东西...

1 个答案:

答案 0 :(得分:2)

[UIImage imageNamed:]将缓存UIImage,如果您不需要缓存它,请使用[UIImage imageWithContentsOfFile:]

您也可以自己使用NSMutableDictionary缓存UIImage,然后在属性时释放NSMutableDictionary