2013-01-04 142 views
1

在我的iPhone应用程序中,我使用自定义的ImageView并使用AsyncImageView类从远程位置加载图像。它可以很好地工作,但是一个问题是,如果我滚动表格,单元格将被取出,并且它会再次尝试从服务器获取图像。所以,从AsyncImageView类加载图像的方法一次又一次地调用,因此增加了内存分配,最终导致应用程序崩溃。在UITableViewCell内存异步图像加载问题

这里是我的代码:

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier { 

    CGRect CellFrame = CGRectMake(0, 0, 300, 40); 

    CGRect userImageFrame = CGRectMake(5, 7, 36, 36); 

    UIImageView *userImage; 

    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; 
    [cell setFrame:CellFrame]; 

    userImage = [[UIImageView alloc]init]; 
    userImage.frame = userImageFrame; 
    userImage.tag = 3; 
    [cell.contentView addSubview:userImage]; 
    [userImage release]; 

    return cell; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(cell == nil) 
     cell = [self getCellContentView:CellIdentifier]; 
    else 
     [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:cell.imageView]; 

    UIImageView *userImage = (UIImageView *)[cell viewWithTag:3]; 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    NSString *url = [[NSString alloc]initWithFormat:@"%@%@", CommonImageURL,[AllUsersProfileImageArray objectAtIndex:indexPath.row]]; 
    NSURL *imageUrl = [NSURL URLWithString:[url stringByAppendingFormat:@"?%i", rand()]]; 
    [url release]; 

    userImage.image = [UIImage imageNamed:@"defaultPerson.png"]; 
    userImage.imageURL = imageUrl; 
    return cell; 
} 

是否有任何可能的方式来解决这个问题?请帮忙。

+0

如果有人需要关于该问题或代码的更多信息,请让我知道。 – Mithuzz

+0

您是在寻找内存泄漏搜索指南还是寻找更好的图像加载解决方案? –

+0

我使用了EGOImageView和现在的AsyncImageView。功能性两者都很好。但问题是内存分配问题。我只是想避免增加的内存分配问题。 – Mithuzz

回答

2

最好的解决方案将缓存已经下载的图像并从那里显示它。

你需要写代码的是,还是有一些其提供这样的特征库:

  1. HJCache
  2. SDWebImage
+0

在更新imageview之前,确保特定“indexPath”的“cell”仍然在屏幕上并且不会重用于另一个“indexPath”是非常重要的。这些异步图像下载器中的许多不这样做,但只是轻松地更新imageview。一个好的实现应该允许一个'completion'块(或委托),我们可以在更新之前确认'indexPath'仍然可见。这些做到这一点吗?这是一个这样的基本问题,当然一些图书馆必须优雅地处理这个问题。哪些库处理最好? – Rob

+0

感谢您的参考,现在我正在使用SDWebImage。但是对于某些类,出现了一些类似Apple Mach-O Linker Error的错误。 – Mithuzz

+1

@Rob:感谢您的宝贵意见,我以前使用过HJCashe,它处理的问题,和一个非常好的库 –

0

我碰到的内存泄漏从服务器加载多个图像的同样的麻烦。我的应用程序每次都需要新的图像响应(功能因子)
 我正在使用NSURLConnection使用异步请求。我试着用

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:0]; 
[NSURLCache setSharedURLCache:sharedCache]; // in DidFinishLoading & didFailWithError 

receivedData=nil; // at didReceiveResponse 
receivedData=[[NSMutableData alloc] init]; 

但没有什么帮助,直到我删除了我的手机 [cell removeFromSuperview];中的cellForRowAtIndexPath并再次初始化它每一个新 NSURL请求(如果附加上的cellForRowAtIndexPath条件,但真的那么所许关)。

可能UIImageViews从未被删除,并且新图像n数据被不断添加,并被响应所取代。删除旧的UITableViewCell为新的NSURLRequest工作在我的情况。
 希望这可以帮助像我这样的人在清理NSURLCache方面失败,并且还会增强内存。