开发者

UITableView with contact image problems

As par my app requirement, I'm showing the contact images in a UITableView as shown below.

-(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:U开发者_JAVA百科ITableViewCellStyleDefault                     
             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;
    }
}

I've two problems if I use the above code segment.

  1. Table Scrolling is too slow. If comment the "adding of image" code, then UITable responds very fast.
  2. Memory Management. My app started using 25 - 30 MB of RAM.

Is there any better way to avoid the above two problems?


You've got a memory leak in the "adding of image" code. ABPersonCopyImageData returns a retained NSData object, which you need to release after you use it to initialize your UIImage, like so:

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

Once you've fixed the leak, image data can be deallocated whenever the corresponding table cell scrolls off of the screen. That should solve your memory and performance issues.


How about pre-caching the images? If you access the Adressbook-Subsystem for every cell it could be the problem? Another issue could be the imageWithData call since the image must be build from scratch every time. There is no image caching here. If you do this before building the table (accessing cellForRow with this) and store the pre-rendered images in a NSMutableDictionary with key ABRecordID?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜