Access contact image from address book based on its id

倾然丶 夕夏残阳落幕 提交于 2020-01-06 22:00:04

问题


As every one is aware of the fact that we can create an address book(ABAddressBook),copy all people in array and then create a record reference(ABRecordRef),then copy its image data using NSData as follows:

ABAddressBookRef addressBook = ABAddressBookCreate( );
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
ABRecordRef recordReference = CFArrayGetValueAtIndex(allPeople, addressIndex);
NSData *imageData = (NSData *)ABPersonCopyImageData(recordReference);

Now this works fine as expected to be a universal fact,but in my case(application),I have a tiny problem,what if the user saves a contact initially and doesn't add/upload any image,then at the time,we are accessing the image data from that contact,we are having empty image,thus I give or assign a default image.But what if the user uploads image for a contact later on or modifies an existing for a contact,which means the updated image will not appear as expected and thus we can't rely on address book name or some index etc..

Hence I came up with an idea of saving the unique contact id,then get the contact image based on its id.In the process I have saved the unique contact id in to database,now what next,holding the id value using model class,then saving that model class in to array.

This is the sample code for understanding:

ReminderClass *reminderToDisplay = [self.remindersArray objectAtIndex:indexPath.row];
NSString *contactIDString = reminderToDisplay.contactID;
int addressIndex = [contactIDString integerValue];

Now as we are aware of the fact that we can have an instance of ABRecordRef and thus copy its image data,but as we know it's syntax would be:

ABRecordRef personID = CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx);

Any way I can assign addressIndex in the place of CFIndex idx

But I am struck with the array part,i.e. CFArrayRef,as I have an NSMutableArray holding all the id details,i.e. remindersArray

Now I have searched for type casting methods from NSMutableArray to CFArrayRef

But found a few questions on vice-versa,a sample question can be seen over here

Please guide me if there is any way to get over this,so that I can gain access to proper images for respective contacts,Any help is greatly appreciated!

Thanks all in advance :)


回答1:


Please find the best solution by Mr.HRM from the below link:

http://www.stackoverflow.com/questions/15138946/access-contact-image-from-address-book-based-on-name

This definitely would assist you the right way,thanks :)



来源:https://stackoverflow.com/questions/11574545/access-contact-image-from-address-book-based-on-its-id

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