2

画像を表示する10個のセルを含むテーブルビューがあります。スクロールするたびに、アプリはより多くのメモリを割り当てます(ただし、これはリークでは表示されません)が、割り当てでは、スクロールごとにメモリが2メガバイトほど増加することがわかります。

これはリークするコードです。具体的には、imageviewの画像を設定した行です(コメントアウトしてもリークしません)。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"background_stripes" ofType:@"png"]];
    return cell;
}

ここに画像の説明を入力してください

更新: 1つのViewControllerを使用して単純な新しいプロジェクトを作成しました。

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.rowHeight = 130.f;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 16;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"background_stripes" ofType:@"png"]];


    return cell;
}

非常に単純です...ここでは問題はありません...しかし、スクロールするとリークが発生し、時間の経過とともにメモリ消費量が増加します...

4

2 に答える 2

1

問題は、漏れがないことです =)。上のグラフは、リークではなく割り当てを示しています。リークは下の図にあり、そのグラフは赤色であるため、メモリ リークはありません。「Overall Bytes」はメモリ リークを表すと思われるかもしれませんが、そうではなく、プログラムが実行時に割り当てたバイト数です。

于 2013-01-15T10:13:24.737 に答える
0

dataWithContentsOfURLでも同じ問題が発生しました:@autoreleasepoolで解決しました

これを試して:

@autoreleasepool {
     self.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"background_stripes" ofType:@"png"]];
}
于 2013-01-14T10:11:04.263 に答える