画像を表示する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;
}
非常に単純です...ここでは問題はありません...しかし、スクロールするとリークが発生し、時間の経過とともにメモリ消費量が増加します...