Oleksandr Matrosov
Oleksandr Matrosov

Reputation: 27133

Set image for contact iOS

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.

Upvotes: 1

Views: 635

Answers (1)

memmons
memmons

Reputation: 40502

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)
}

Upvotes: 1

Related Questions