1

我正在使用以下代码使用以下代码从服务器加载图像。当我滚动 UITableView 应用程序崩溃时。

AsynchrhousImageView 类 .m 文件

- (void)dealloc {
[connection cancel]; //in case the URL is still downloading
[connection release];
[data release]; 
[_imageView release];
[_activityIndicator release];
[super dealloc];
}

- (void)loadImageFromURL:(NSURL*)url 
   defaultImageName:(NSString *)defaultImageName 
   showDefaultImage:(BOOL)defaultImageIsShown 
   showActivityIndicator:(BOOL)activityIndicatorIsShown 
   activityIndicatorRect:(CGRect)activityIndicatorRect
   activityIndicatorStyle:(UIActivityIndicatorViewStyle)activityIndicatorStyle {

if (connection!=nil) { [connection release]; }  if (data!=nil) { [data release]; }

 if ([[self subviews] count]>0) {
    [[[self subviews] objectAtIndex:0] removeFromSuperview]; // }

 if (defaultImageIsShown) {
      self.imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:defaultImageName]] autorelease];
} else {
    self.imageView = [[[UIImageView alloc] init] autorelease];
}


[self addSubview:_imageView];
_imageView.frame = self.bounds;
[_imageView setNeedsLayout];   
[self setNeedsLayout];

if (activityIndicatorIsShown) {

    self.activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:activityIndicatorStyle] autorelease];
    [self addSubview:_activityIndicator];
    _activityIndicator.frame = activityIndicatorRect;
    _activityIndicator.center = CGPointMake(_imageView.frame.size.width/2, _imageView.frame.size.height/2);
    [_activityIndicator setHidesWhenStopped:YES];
    [_activityIndicator startAnimating];
}


NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
 }


- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData  *)incrementalData {
if (data==nil) { data = [[NSMutableData alloc] initWithCapacity:2048]; } 
[data appendData:incrementalData];
  }

  - (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
  [connection release];
  connection=nil;

   _imageView.image = [UIImage imageWithData:data];

if (_activityIndicator) {
    [_activityIndicator stopAnimating];
}

[data release];     data=nil;
}


- (UIImage*) image {
UIImageView* iv = [[self subviews] objectAtIndex:0];
return [iv image];
}

在加载图像的 ViewController 类中

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



static NSString *reuseIdentifier =@"CellIdentifier";
    ListCell *cell = (ListCell *)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (cell==nil) {
        cell = [[ListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        NSMutableDictionary *dicResult = [arrResults objectAtIndex:indexPath.row];

    NSURL *url=[NSURL URLWithString:[dicResult objectForKey:@"Image"]];
    AsynchronousImageView *asyncImageView = [[AsynchronousImageView alloc] initWithFrame:CGRectMake(5, 10,80,80)];
    [asyncImageView loadImageFromURL:url
                    defaultImageName:@"DefaultImage.png"
                    showDefaultImage:NO
               showActivityIndicator:YES
               activityIndicatorRect:CGRectMake(5, 10,30,30)
              activityIndicatorStyle:UIActivityIndicatorViewStyleGray]; // load our image with URL asynchronously

    [cell.contentView addSubview:asyncImageView];   
    // cell.imgLocationView.image = [UIImage imageNamed:[dicResult valueForKey:@"Image"]];
    [asyncImageView release];

}

if([arrResults count]==1)

{
    UITableViewCell *cell1=[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if(cell1==nil)
        cell1=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] autorelease];
    NSMutableDictionary *dicResult = [arrResults objectAtIndex:0];
    cell1.textLabel.text=[dicResult valueForKey:@"NoResults"];
    return cell1;
}
else
{
NSMutableDictionary *dicResult = [arrResults objectAtIndex:indexPath.row];
NSString *title = [NSString stringWithFormat:@"%@ Bedrooms-%@", [dicResult valueForKey:KEY_NUMBER_OF_BEDROOMS],[dicResult valueForKey:KEY_PROPERTY_TYPE]];
NSString *strAddress = [dicResult valueForKey:KEY_DISPLAY_NAME];
NSString *address = [strAddress stringByReplacingOccurrencesOfString:@", " withString:@"\n"];
NSString *price = [dicResult valueForKey:KEY_PRICE];
NSString *distance = [dicResult valueForKey:KEY_DISTANCE];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        cell.lblTitle.text = title;
cell.lblAddress.text = address;
if ([price length]>0) {
    cell.lblPrice.text = [NSString stringWithFormat:@"£%@",price];
}else{
    cell.lblPrice.text = @"";
}
if ([distance length]>0) {
    cell.lblmiles.text = [NSString stringWithFormat:@"%.2f miles",[distance floatValue]];
}else{
    cell.lblmiles.text = @"";
}


}
return cell;
}

我该如何解决这个问题?我附上了它的heapshot分析屏幕截图。这里非对象消耗了这么多内存那是什么?堆射击分析

4

2 回答 2

5

这是错误:

NSString *reuseIdentifier = [NSString stringWithFormat:@"%d",indexPath.row];

看来您不是在重用单元格,而是为表格的每一行创建一个新单元格!!!这样,如果您需要查看 100 或 1000 行,则创建/分配 100 或 1000 个对象单元格。

这不是 UITableView 的正确使用。UITableView 的“魔力”在于它重用单元格,它只是创建和分配所需的少量单元格......

例如,考虑您的表格有 480 像素的垂直空间,并且您的单元格高度为 100 像素,那么您只需要 5 个单元格,无需创建 1000 个单元格,您一次只能看到 5 个单元格...

所以神奇的是当你向上滚动它并离开屏幕时重用已经分配的单元格,并重置它的内容(图像和文本)并将其用于用户将要看到的新呼叫......

于 2012-04-06T11:10:34.627 回答
2

虽然细胞重用不是问题,但泄漏细胞是:

cell = [[ListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];

你忘记了autorelease这一点,所以你很快就会泄漏细胞。你确实记得 autorelease cell1

于 2012-04-06T12:51:24.130 回答