2010-03-14 93 views
0

由於我的應用程序要求,我顯示在UITableView中的聯繫人圖像如下所示。UITableView與聯繫人圖像問題

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath 
                   (NSIndexPath *)indexPath 
{ 
    //NSMutableArray *sourceArray = sourceList; 
    static NSString *identifier = @"CellIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 
    if (cell == nil) 
    { 
     //If not possible create a new cell 
     cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault      
      reuseIdentifier:identifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
     cell.opaque = NO; 

     CGRect LabelFrame = CGRectMake(130, 33, 150, 13); 
     callImage = [[UIImageView alloc] initWithFrame:LabelFrame];//phone image 
     callImage.tag = 1; 
     [cell.contentView addSubview:callImage]; 
     [callImage release]; 
      ----- 
      ----- 
    } 
    UIImageView *callImage = (UIImageView *)[cell viewWithTag:1]; 
    ABRecordRef contact = [self getContact]; 
    if(contact && ABPersonHasImageData(contact)) 
    { 
     UIImage *contactImage = [UIImage imageWithData:(NSData*)ABPersonCopyImageData(contact)]; 
     callImage.image = contactImage; 
    } 
} 

我有兩個問題,如果我用上面的代碼段。

  1. 表滾動速度太慢。如果評論「圖像添加」代碼,那麼UITable響應速度非常快。
  2. 內存管理。我的應用程序開始使用25 - 30 MB的RAM。

有沒有更好的方法可以避免上述兩個問題?

+0

你能發佈你的cellForRowAtIndexPath方法的完整代碼嗎?你的問題可能是你沒有正確地重用你的單元格 – Vladimir 2010-03-14 09:26:23

+0

是的,我正在以適當的方式重用它們。無論如何,我發佈在我的上述問題本身。 – prathumca 2010-03-15 14:46:37

回答

1

「添加圖像」代碼中存在內存泄漏。 ABPersonCopyImageData返回保留NSData對象,你需要釋放你使用它後初始化的UIImage,像這樣:一旦你已經解決了泄漏

NSData *contactImageData = (NSData*)ABPersonCopyImageData(contact); 
UIImage *contactImage = [UIImage imageWithData:]; 
[contactImageData release]; 

,圖像數據可以被釋放,只要相應的表格單元格滾動關閉屏幕。這應該可以解決你的記憶和性能問題。

+0

我已經試過了。但沒用 – prathumca 2010-03-16 04:16:11

0

預先緩存圖像如何?如果您爲每個單元訪問Adressbook-Subsystem,可能會出現問題? 另一個問題可能是imageWithData調用,因爲每次都必須從頭開始構建圖像。這裏沒有圖像緩存。如果在構建表之前執行此操作(使用此方法訪問cellForRow)並將預渲染的圖像存儲在帶有ABRecordID鍵的NSMutableDictionary中?