How do I use a contact's photo in a table view cell?
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 sto开发者_运维技巧red 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.
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)
精彩评论