2011-12-07 17 views
1

imageNamedではなくimageWithContentsOfFileを使用すると、UITableViewのスクロールでパフォーマンスの問題が発生します。私のコードを以下に示します。iPhone:imageWithContentsOfFileのUITableViewパフォーマンスの問題

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

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
UniversalAppAppDelegate *delegate = (UniversalAppAppDelegate *) [[UIApplication sharedApplication] delegate]; 
NSDictionary *res = [[delegate comments] objectAtIndex:indexPath.section]; 

if (cell == nil) 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 

...... 

// This line has a problem...when I use imageNamed here instead of imageWithContentsOfFile, it works absolutely fine 
imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:imagepath]]; 
[imageView setFrame: CGRectMake(30, 10, 55, 55)]; 
[sectionHeaderView addSubview:imageView]; 

...... 

cell.backgroundView = sectionHeaderView; 
return cell; 

私もcaching画像が、それでも同じ問題を試してみました。 imageNamedのみが正常に動作しますが、ドキュメントフォルダに格納されたイメージのパスを指定して動的に変更する必要があるため、使用できません。誰も私にこの問題を解決する方法を教えてもらえますか?

キャッシュコード:

NSMutableDictionary *thumbnailCache = [[NSMutableDictionary alloc] init]; 

- (UIImage*)thumbnailImage:(NSString*)fileName 
{ 
UIImage *thumbnail = [thumbnailCache objectForKey:fileName]; 

if (nil == thumbnail) 
{ 
    NSString *thumbnailFile = [self filePath:fileName]; 
    thumbnail = [UIImage imageWithContentsOfFile:thumbnailFile]; 
    [thumbnailCache setObject:thumbnail forKey:fileName]; 
} 
return thumbnail; 
} 

使用法:

imageView = [[UIImageView alloc] initWithImage:[delegate thumbnailImage:[res objectForKey:@"ImageName"]]]; 

答えて

2

あなたは、コントローラのviewDidLoad方法でUIImagesの配列を構築してみてください、そして、あなたのcellForRowAtIndexPathにその配列内の画像を使用することができます。

これにより、テーブルビューを描画するときに、各画像にimageWithContentsOfFileを呼び出す必要がなくなります。

たとえば、別のスレッドで配列を作成し、ロードインジケータを表示することができます。最初は小さな読み込み時間を意味しますが、滑らかなスクロールテーブルビューを意味します。

+0

これは私がキャッシングすることを意味しています。私はすでにそれをやっているが、それはどちらも役に立たなかった。私のキャッシュコードを追加しました。 – applefreak

+0

ありがとうございました。キャッシュを使用しているときに 'cellForRowAtIndexPath'の内部で何を変更しましたか? – Mutix

+0

私の悪い!私はこの辞書を初期化するのを忘れていました。初期化され、その後正常に動作しました! – applefreak