Access contact image from address book based on name

 ̄綄美尐妖づ 提交于 2019-12-08 19:03:30

I am sharing the sample code snippet I used in one of my recent app. I have modified to fit it ur requirements and also please note that I have edited this in notepad and may have some typo errors.(Currently I dnt have mac to test it..:P) Basic idea is to fill the datasource in viewDidLoad method and use that dataSource to update the tableView. Hope this will be an input to solve your problem.

viewDidLoad

contactsToBeAdded=[[NSMutableArray alloc] init];
ABAddressBookRef addressbook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressbook);
CFIndex numPeople = ABAddressBookGetPersonCount(addressbook);
bool hasPhoneNumber = false;
for (int i=0; i < numPeople; i++) {
    hasPhoneNumber = false;
    ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
    ABMutableMultiValueRef phonelist = ABRecordCopyValue(person, kABPersonPhoneProperty);
    CFIndex numPhones = ABMultiValueGetCount(phonelist);


    if(numPhones > 0){
        hasPhoneNumber = true;

    }
    if(hasPhoneNumber){
        NSString *firstName=(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
        NSString *lastName=(NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

        CFTypeRef ABphone = ABMultiValueCopyValueAtIndex(phonelist, 0);
        NSString *personPhone = (NSString *)ABphone;

        NSMutableDictionary *dictToAdd = [[[NSMutableDictionary alloc]init]autorelease];
        if(firstName != nil && firstName != NULL){
            [dictToAdd setObject:firstName forKey:@"firstName"];
            CFRelease(firstName);
        }
        else{
            [dictToAdd setObject:@"" forKey:@"firstName"];
        }
        if(lastName != nil && lastName != NULL){
            [dictToAdd setObject:lastName forKey:@"lastName"];
            CFRelease(lastName);
        }
        else{
            [dictToAdd setObject:@"" forKey:@"lastName"];
        }

        if(personPhone != nil && personPhone != NULL){
            [dictToAdd setObject:personPhone forKey:@"mobile"];
            CFRelease(ABphone);
        }
        else{
            [dictToAdd setObject:@"" forKey:@"mobile"];
        }

        //Get the first name and last name added to dict and combine it to full name
        NSString *firstName = [dictToAdd objectForKey:@"firstName"];
        NSString *lastName = [dictToAdd objectForKey:@"lastName"];
        NSString *fullName = [firstName stringByAppendingString:lastName]; 

        //Now check whether the full name is same as your reminderToDisplay.Name
        if(reminderToDisplay.Name isEqualToString:fullName )
        {
            CFDataRef imageData = ABPersonCopyImageData(person);
            UIImage *image = [UIImage imageWithData:(NSData *)imageData];
            if(image != nil && image != NULL){
                [dictToAdd setObject:image forKey:@"image"];
                CFRelease(imageData);
            }
            else{
                [dictToAdd setObject:[UIImage imageNamed:TEMP_IMG] forKey:@"image"];
            }
        }

        [contactsToBeAdded addObject:dictToAdd];
    }

    CFRelease(phonelist);
}
CFRelease(allPeople);
CFRelease(addressbook);

[self.tableView reloadData];

numberOfRowsInSection

return contactsToBeAdded.count;

cellForRowAtIndexPath

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];

}

NSDictionary *contactToAdd;
//This way you can get the data added in viewDidLoad method
contactToAdd = [contactsToBeAdded objectAtIndex:indexPath.row];

NSString *fName = (NSString *)[contactToAdd objectForKey:@"firstName"];
NSString *lName = (NSString *)[contactToAdd objectForKey:@"lastName"];
UIImage *contactImg = (UIImage*)[contactToAdd objectForKey:@"image"];

when you use 'ABAddressBookCopyArrayOfAllPeople' you get an array of persons in the addressbook. I believe they are type of ABPerson.

You can now loop over them list like you are. For each one record call 'ABPersonCopyImageData' and that will give you the image data as a CFDataRef.

And remember CFDataRef is a tool free bridge to NSData.

Just try changing your code like this. You are adding dictionary items to ur contactsList, so get each dictionary and check whether it contains a key matching your reminderToDisplay.Name, if yes then do ur stuff..

for (NSDictionary* dict in contactsList)
{
   if(nil != [dict objectForKey:reminderToDisplay.Name])
   {
     //take image here
   }
}

UPDATE:

//This is the reminder's name you want to get contact image
ReminderClass *reminderToDisplay = [self.remindersArray objectAtIndex:indexPath.row];

 //Here you are adding your contacts with full name as object and kName as key, but check ur kName here
 NSDictionary *contactsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:self.contactName, kName, [NSNumber numberWithInt:i], kIndex, nil]; 
 [self.contactsList addObject:contactsDictionary];

 //I dont think this part is needed in ur code
 NSDictionary *contactsDictionary = [self.contactsList objectAtIndex:indexPath.row]; 
 self.contactName = [contactsDictionary objectForKey:kName]; 
 int addressIndex = [[contactsDictionary objectForKey:kIndex]intValue];

Now you have your contact names as a dictionary in contactsList array, iterate the array and check whether the dictionary contains your reminderToDisplay.Name as key.

for (NSDictionary* dict in contactsList)
{
    //Please note that your dict contains key as kName and object as contact name.
    if(nil != [dict objectForKey:reminderToDisplay.Name])
    {
    }
}

Also, I feel like you can do this in one single loop, like when you are iterating the addressbook itself, you can check whether the contact name is in your reminderlist and if available then extract image.

Hope this helps..all the best...

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