tags:

views:

84

answers:

1

I've got an app that has a table view that displays contact information in each row. I'd like to use the contact's stored image (if there is one available) as the image on the left-hand side of the cell.

I've found some sketchy sample code in Apple's documentation, but the address book references some kind of weird data type (CFDataRef) that doesn't appear to correspond to the data types referenced in the table view programming guide (mainly UIImage).

This seems like a pretty basic task, but I can't seem to wrap my head around it. Thanks in advance for any help you can offer.

+1  A: 

You can use the ABPersonHasImageData function to check if the contact has an image.

Here's an example:

- ( UITableViewCell *) tableView: ( UITableView *) tableView cellForRowAtIndexPath: ( NSIndexPath *) indexPath {
static NSString *id = @"id";
UITableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier: id ];

if( !cell ) cell = [[ UITableViewCell alloc ] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: id ];

ABRecordRef record = ...

UIImage *image;
if( ABPersonHasImageData( record ) ) {
   //record has an image
   image = [ UIImage imageWithData: ABPersonCopyImageData( record ) ];
} else {
   image = [ UIImage imageNamed: @"silhouette.png" ]; //just an example
}

cell.imageView.image = image;

// do other stuff with cell...


return cell;
}

Core Foundation types are toll-free bridged with the Foundation types (CF==NS)

Jacob Relkin
Awesome! Exactly what I needed, and works like a charm. Thanks for the quick help!
Andy
@Andy, you're welcome!
Jacob Relkin