2

データモデルを使用して、ビデオ、画像の 2 つのオブジェクトを格納します。ビデオには文字列属性のみが含まれ、画像には 2 つの「バイナリ データ」属性があります。

最初は、2 つのバイナリ データ属性がビデオ オブジェクトにありました。ただし、UITableView の初期化中にすべてのビデオが読み込まれます。400 本のビデオの場合、バイナリ データは 20 Mo を表すため、4000 本のビデオを想像してみてください...

2 つのオブジェクトを使用すると、UITableView の読み込みがうまく機能します。メソッドで必要な場合は、バイナリ データを読み込みます: tableView:cellForRowAtIndexPath

しかし、リストをスクロールすると、メモリが増えます:(

私の方法を見てください:

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

    static NSString *CellIdentifier = @"videoCell";
    Video *theVideo = (Video *)[[self fetchedResultsController] objectAtIndexPath:indexPath];
    VideoCellViewController *cell = (VideoCellViewController *)[myTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"VideoCellView" owner:self options:nil];
        cell = editingTableViewCell;
        self.editingTableViewCell = nil;
    }
    cell.video = theVideo;
    return cell;
}

そして、VideoCellViewController の setvideo メソッド

- (void)setVideo:(Video *)newVideo {
    if (newVideo != video) {
        [video release];
        video = [newVideo retain];
    }
    NSData *imageData = [video.allImages valueForKey:@"thumbnailImage"];
    UIImage *uiImage = [[UIImage alloc] initWithData:imageData];
    smallImage.image = uiImage;
    nameLabel.text = video.displayName;
    [uiImage release];
}

smallImage を設定しなくても、メモリに問題があります。画像オブジェクトをロードすると、決して解放されません。

私は成功せずにメモリを解放するために多くの解決策を試みます...( didTurnIntoFault、release、CFRelease...) パフォーマンス ツールでは、バイナリ データが CFData として表示されます。

iPhoneCoreDataRecipes と PhotoLocations のサンプルをよく使用します。

記憶をきれいにするために助けが必要です;)

ありがとう

サミュエル

4

1 に答える 1

1

明らかに、テーブル セルの作成ロジックで何かが起こっています。cellForRowまず、典型的なデリゲート ハンドラーを見てみましょう。

static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
// do stuff with cell
return cell;

ここにいることがわかります

  • 再利用可能なセルを取得しようとしています
  • それが失敗した場合 (nil) 新しいものを作成し、再利用可能な ID を ctor に渡します
  • 次に、セル(新規または既存)で処理を行い、それを返します

テーブル ビューで再利用するためにセルにキーを設定しないと、デキューから常に「nil」セルが返されるため、毎回新しいセルを作成する必要があります。これにより、スクロールするにつれてメモリが増加し続けますが、アイドル状態のときはかなりフラットなままです。

編集:

セルに問題がないと仮定すると、リークしているのがビデオ データなのか画像データなのかを絞り込む必要があります。とはsmallImage? そして、ビデオが新しいときだけすべてをやりたくないですか?

- (void)setVideo:(Video *)newVideo {
    if (newVideo != video) {
        [video release];
        video = [newVideo retain];
        NSData *imageData = [video.allImages valueForKey:@"thumbnailImage"];
        UIImage *uiImage = [[UIImage alloc] initWithData:imageData];
        smallImage.image = uiImage;
        nameLabel.text = video.displayName;
        [uiImage release];
    }
}
于 2010-01-22T18:56:02.817 に答える