问题
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!
回答1:
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.
回答2:
Try:
CFStringRef firstName, lastName; // can cast this to NSString *
firstName = ABRecordCopyValue(aABRecordRef, kABPersonFirstNameProperty);
lastName = ABRecordCopyValue(aABRecordRef, kABPersonLastNameProperty);
来源:https://stackoverflow.com/questions/14259332/get-a-contact-picture-ios