Question

I am using ABPeoplePickerNavigationController for representation table of contacts. By tapping contact I need to set new image for it. I added code to delegate for changing person data, but can't change image. Any suggestions?

This is my code below:

 - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    NSData *dataRef = UIImagePNGRepresentation(self.theImage);
    CFDataRef cfdata = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);
    CFErrorRef error;

    ABPersonRemoveImageData(person, &error); // clean any image first from ref
    if (ABAddressBookSave(_addressBook, &error))
    {
        ABPersonSetImageData(person, cfdata, &error);
        ABAddressBookSave(_addressBook, &error);
    }
    CFRelease(cfdata);

    [self dismissViewControllerAnimated:YES completion:nil];

    return NO;
}

I downloaded sample from here: link

To check how it works you can download code and modify delegate below with my code.

Was it helpful?

Solution

You need to save the changes via ABAddressBookSave()

Also, keep in mind if your contact does NOT already have an image, both the thumbnail and the full sized image will be added when you use ABPersonSetImageData. However, if your contact has a full-sized image already, only the thumbnail will be set when you useABPersonSetImageData.

// this is not production level code. method call return values and errors
// need to be handled properly
ABPersonRemoveImageData(person, &error); // clean any image first from ref
if (ABAddressBookSave(addressBook, &error))
{
   ABPersonSetImageData(person, cfdata, &error);
   ABAddressBookSave(addressBook, &error)
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top