Get a Contact Picture ios

淺唱寂寞╮ 提交于 2019-12-10 11:29:15

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!