2

I am trying to figure out how to add persons last name and first name in this code below in order to get their contact image from my phone:

if(ABPersonHasImageData(aABRecordRef))
{
   UIImage *image = [UIImage imageWithData:(NSData *)ABPersonCopyImageData(aABRecordRef)];
   cell.imageView.image = image;
} else {
   UIImage *image1=[UIImage imageNamed:@"User.jpg"];
   cell.imageView.image=image1;
}

I could also be missing code here so please do fill in the areas where it is needed. I just cant figure out how to add the first,last name to the code above so i know when i have reached that contact to extract the image for it.

Any help would be great! Thanks!

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
StealthRT
  • 10,108
  • 40
  • 183
  • 342

2 Answers2

4

To get the FirstName and LastName , you can use :

NSString *firstName = (NSString *)ABRecordCopyValue(aABRecordRef, kABPersonFirstNameProperty);
NSString *lastName = (NSString *)ABRecordCopyValue(aABRecordRef, kABPersonLastNameProperty);

Here it shows how to Search By Number and Get the image using ABAddressBook.

But you want to search By FirstName and LastName. So According to the Documentation , you should use -recordsMatchingSearchElement: method for Multiple Arguments.

Once you get the matched Data , you can extract the image using below code :

CFDataRef imageData = ABPersonCopyImageData(aABRecordRef);
UIImage *image = [UIImage imageWithData:(NSData *)imageData];
CFRelease(imageData);

Hope you get something useful from this.

Community
  • 1
  • 1
Bhavin
  • 27,155
  • 11
  • 55
  • 94
0

Try:

CFStringRef firstName, lastName; // can cast this to NSString *

firstName = ABRecordCopyValue(aABRecordRef, kABPersonFirstNameProperty);
lastName = ABRecordCopyValue(aABRecordRef, kABPersonLastNameProperty);  
Bejmax
  • 945
  • 7
  • 16