2011-12-15 14 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