2011-12-15 15 views
0

我在我的項目中使用了ASIHTTPRequest框架來處理所有與網絡有關的任務。ASIHTTPRequest同時下載桌面視圖上的圖像需要時間

我有自定義單元格與來自web服務器的縮略圖,有大約500個圖像,所以我不得不重複使用單元格來處理它。當我們滾動瀏覽tableview時,由於重複使用單元格,我們可以看到之前單元格的圖像,這些圖像將被新圖像替換。

如果網絡連接低,它更糟糕,因爲它需要大量的時間來下載圖像..所以當時你可以看到特定的錯誤圖像,因爲重新使用單元格,所以我需要找到方法,以便這種圖像替換不應該'用戶不可見。

我正在使用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 2011-12-15 05:08:06

回答

0

數據我已經使用ASIDownloadCache的方法來解決我的問題。其實有這個問題

  1. 2個解決方案設置自己的緩存路徑,而不是使用SharedCache的,但我沒有去這個監守我已經使用sharedCache並發現了另一個有效的方法,這將避免我改變我目前的實施

  2. 在這種方法我都用過,從ASIDownloadCache方法2種方法(令人驚訝的ASIHTTPREquest網站沒有提到在他們的簡短信息,這些方法)

    2。1第一種方法- (BOOL)isCachedDataCurrentForRequest:(ASIHTTPRequest *)request
    覈實,如果該特定圖像的URL已經緩存與否,如果是使用第二個方法 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

在你的tableview細胞的委託,當你得到重用或新的細胞,它返回之前清除圖像。在異步回調中使用正確的自帶映像進行更新。您可能想要確保圖像保存或保留在其他地方,但如果您不希望您的應用程序保持紅色下載它們。

在ASIHTTPRequest框架
+0

我已經實現共享緩存與代表,但我用這個委託是需要時間去思考作爲即時通訊的代碼顯示圖像其速度不如文本... – 2011-12-15 06:52:36

0

其兩個工種同步和ASynchronize所以FIRAT,專門告訴我u使用其中一個用於獲取圖像數據&還告訴我,美在一次發送整個500圖像請求,或者按您的單元被裝載

或者如果您發送500張圖像的請求比這個要求不正確,請按照小區的要求發送請求,否則這個小區圖像不可行。

+0

我發送8個併發請求... – 2011-12-15 06:53:18

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 

這將是你如何重裝你的桌子上

- (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

我已經實現了與委託相同的方式,但現在...當我滾動需要一段時間來加載圖像的單元格..因爲我有其他文字在任何時候都被替換,但不會發生與圖像相同... – 2011-12-15 06:51:07