2011-12-15 6 views
0

私は自分のプロジェクトでASIHTTPRequestフレームワークを使用して、すべてのネットワーク関連タスクを処理しています。ASIHTTPRequestテーブルビューでの画像の同時ダウンロードには時間がかかります

私はWebサーバーから来ているサムネイルを持つカスタムセルを持っているので、それを処理するためにセルを再利用する必要があります。テーブルビューをスクロールするときにセルが再利用されるため、新しい画像に置き換えられる前のセルの画像が表示されます。

画像をダウンロードするのに時間がかかるのでネットワーク接続が悪い場合は...その時間は間違った画像が表示されることがあります。再利用するセルなので、この画像の置き換えは必要ありません。ユーザーに見えるようにする。

私はASIDownalod SharedCacheメソッドを使用しています。あなたのテーブルビューセルのデリゲートで

NSString *reuseIdentifier = @"offerCell"; 

BRZOfferCell *offerCell = (BRZOfferCell*)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; 

if (offerCell==nil) { 

    offerCell = [[[BRZOfferCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier celltype:kDealCellTypeDealsList] autorelease]; 
} 

[offerCell setImage:[UIImage imageNamed:IMAGE_NO_IMAGE]]; 

//---get the letter in the current section--- 
//NSString *alphabet = [mDealsIndex objectAtIndex:[indexPath section]]; 

//---get all deals beginning with the letter--- 
NSString* lSectionIndex = [NSString stringWithFormat:@"%i",[indexPath section]]; 

NSMutableArray *deals = [mIndexedOffersDic objectForKey:lSectionIndex]; 


if ([deals count]>0) { 
    //---extract the relevant deal from the deals array object--- 
    Offer* lOffer = [deals objectAtIndex:indexPath.row]; 

    [offerCell setOffer:lOffer]; 

    offerCell.accessoryView = UITableViewCellAccessoryNone; 

    if (mTableView.dragging == NO && mTableView.decelerating == NO) 
    { 
     //Function : format image url to [email protected] and Initiate Image request download 
     //and set cache policy 
     [mListViewHelper InitImageRequest: lOffer.PromoImage indexPath: indexPath]; 
    } 

} 

return offerCell; 
+0

は、それは私がデリゲートと共有キャッシュを実装しているが、私はそれが時間がかかり、このデリゲートを使用してイムとして考えるあなたのUITableViewのデリゲートメソッドのため – aryaxt

答えて

0

あなたのテーブルの上には、データをリロードする方法だろう。実際にそこSharedCacheを使用する代わりに、独自のキャッシュパスを設定し、この問題

  1. のための2つのソリューションがありますが、私はすでにsharedCacheを使用していたこのbecuaseのために行って、私の現在を変え、私を避けることができます別の効率的な方法が見つかりませんでしたこのアプローチでは、実装

  2. 私はASIDownloadCacheメソッドからの2つの方法を使用している

    2(驚くほどASIHTTPREquestウェブサイトは、その簡潔な情報でこれらのメソッドについては言及しませんでした)。1最初のメソッド- (BOOL)isCachedDataCurrentForRequest:(ASIHTTPRequest *)request
    この特定の画像URLがすでにキャッシュされているかどうかを確認してください.2番目のメソッド 2.2 - (NSData *)cachedResponseDataForURL:(NSURL *)urlを使用してキャッシュされた画像を取得し、cellForRowAtIndexPath自体に画像を設定できます。細胞の再利用性。ここで

コードです:

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    NSString *reuseIdentifier = @"offerCell"; 


BRZOfferCell *offerCell = (BRZOfferCell*)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; 

if (offerCell==nil) { 

    offerCell = [[[BRZOfferCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier celltype:kDealCellTypeDealsList] autorelease]; 
} 

[offerCell setImage:[UIImage imageNamed:IMAGE_NO_IMAGE]]; 

//---get the letter in the current section--- 
//NSString *alphabet = [mDealsIndex objectAtIndex:[indexPath section]]; 

//---get all deals beginning with the letter--- 
NSString* lSectionIndex = [NSString stringWithFormat:@"%i",[indexPath section]]; 

NSMutableArray *deals = [mIndexedOffersDic objectForKey:lSectionIndex]; 


if ([deals count]>0) { 
    //---extract the relevant deal from the deals array object--- 
    Offer* lOffer = [deals objectAtIndex:indexPath.row]; 

    [offerCell setOffer:lOffer]; 

    offerCell.accessoryView = UITableViewCellAccessoryNone; 

    if ([mListViewHelper isCached:lOffer.PromoImage]) { // Is image available in Cache ? 

     // Image is available use image fomr cache directly 
     [offerCell setImage:[UIImage imageWithData:[mListViewHelper cacheDataWithNSURL:lOffer.PromoImage]]]; 
    } 
    else{ 
     //Function : Initiate Image request download and set cache policy 

     if (mTableView.dragging == NO && mTableView.decelerating == NO) 
      [mListViewHelper InitImageRequest: lOffer.PromoImage indexPath: indexPath]; 
    } 
} 

return offerCell; 
} 
0

EDIT、あなたは再利用したり、新しい細胞を得るとき、それを返す前に画像を消去します。非同期コールバックで適切な独自のイメージで更新します。ただし、画像を保存し直したり、画像を保存し直したりしたくない場合は、画像を保存したり、別の場所に保存したりすることをおすすめします。 ASIHTTPRequestの枠組みの中で

+0

をコードを確認するのに役立つだろう画像をそれほど高速ではなくテキストとして表示します... –

0

タイプ同期とASynchronizeの両方でその作業のでfiratはuが&もuが一度に全体500画像要求を送信したり、セルごとに送信することを教えゲットイメージデータに使用され1を教え

をロードされ、

または500以上の画像を一度に送信した場合は、セルの要件に従って適切ではない場合は、そのセル画像を別の方法で送信してください。

+0

私は8つの同時リクエストを送信しています... –

1

あなたが言ったように、UITableViewはパフォーマンスを上げるためにセルを再利用するため、再利用する前にセルをクリアする必要があります。間違ったデータが表示されることがあります。

また、非同期呼び出しと一部の委任を使用してセルを更新する必要があります。

私は実際にはレベルを上げてNSOperationQueueを使用して、同時ダウンロードの最大数を設定し、ページから離れるときにリクエストをキャンセルすることができます。あなたがしたいかもしれないもの

データヘルパーを作成することです
@protocol BookDataHelperDelegate <NSObject> 
@required 
- (void) bookDataHelperDidLoadImage:(BookDataHelper *)dataHelper; 
@end 

@interface BookDataHelper 

@property (nonatomic, retian) UIImage *bookCover; 
@property (nonatomic, retain) Book *book; 
@property (nonatomic, assign) NSObject<BookDataHelperDelegate> *delegate; 

- (void) fetchImageAsynchronouslyFromWebWithDelegate:(NSObject<BookDataHelperDelegate> *)delegate; 

@end 

これは私が私の問題を解決するためにASIDownloadCacheメソッドを使用している

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

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 
    CustomCell *cell = [tableView 
    dequeueReusableCellWithIdentifier:SimpleTableIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault 
     reuseIdentifier:SimpleTableIdentifier] autorelease]; 
    } 

    BookDataHelper *dataHelper = [myArray objectAtIndex:indexPath.row]; 

    if (!dataHelper.bookCover) 
    { 
     [cell.imageView setImage:nil]; 
     [dataHelper fetchImageAsynchronouslyFromWebWithDelegate:self]; 
    } 
    else 
    { 
     [cell.imageView setImage:dataHelper.bookCover]; 
    } 

    cell.bookTitleLabel.text = dataHelper.book.title; 
    return cell; 

} 

- (void)bookDataHelperDidLoadImage:(BookDataHelper *)datahelper 
{ 
    [tableView reloadDate]; 
    // here you would either reload the table completely 
    // Or you could reload specific cells 
} 
+0

私はデリゲートと同じ方法を実装しましたが、今は...スクロールするときに、画像をセルにロードするのに時間がかかります。私は他のテキストを持っているので、時間がないにもかかわらず、画像と同じことは起こっていません... –

+0

itダウンロードした画像を配列に保存しているあなたのコードにひっくり返っていますか? –

+0

あなたはこれらのイメージを配列オブジェクトに格納するとメモリの問題は発生しません。私の場合は500個のイメージがあります.. –

関連する問題