I'm trying to set the contact image with the code below. I am not seeing any errors, and the image is not saved to the contact entry in the address book. Please help! I must be doing something wrong...
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
[abcontroller setDisplayedPerson:person];
UIImage *im = [UIImage imageNamed:@"image1.jpg"];
NSData *dataRef = UIImagePNGRepresentation(im);
CFErrorRef error;
ABAddressBookRef addressBook = ABAddressBookCreate();
NSLog(@"Error:",error);
if (ABPersonSetImageData(person, (CFDataRef)dataRef, &error))
{
NSLog(@"Set contact photo %@", error);
if (ABAddressBookHasUnsavedChanges(addressBook))
{
NSLog(@"Changes made to address book");
}
else {
NSLog(@"No changes made to address book");
}
if (ABAddressBookSave(addressBook, &error))
{
NSLog(@"Saved");
}
else {
NSLog(@"Not saved");
}
}
else {
NSLog(@"Error saving contact photo %@", error);
}
ABAddressBookSave(addressBook, &error);
[self dismissModalViewControllerAnimated:YES];
return NO;
}
Here is my output log:
2010-01-17 21:58:35.465 Error:
2010-01-17 21:58:35.504 Set contact photo <ABPeoplePickerNavigationController: 0x19e9b0>
2010-01-17 21:58:43.497 No changes made to address book
2010-01-17 21:58:44.724 Saved
I'm not sure why the error object is logging as an ABPeoplePickerNavigationController object?
All these answer didn't work. You should simply change your line of code from:
to:
This way you get the reference to the "address book", "person" belongs to.
I have also try to use to set the image (Photo) of current selected person but it display massage to saved successfully but in real it dose not work!!!
after the digging out the code and the documents provided by Apple SDK, i have some up with the following solution.
Please check out the below code. its really working fine for me and hope for you also.
I checked your code and it doesn't work as it should. So I made 2 changes.
Now it works as it supposed to.
Regarding I'm not sure why the error object is logging as an ABPeoplePickerNavigationController object? -- because you're not properly initialising the
error
here:Assign
nil
here, or it will have a random (or more precisely, some previous memory) value, incidentally pointing to anABPeoplePickerNavigationController
object.Regarding the merits: are you sure the reference to
person
you have passed to your method is valid in the context of the address book? I would try to use a function such asABAddressBookGetPersonWithRecordID
first, instead of passingABPersonRef
s around and expecting them to be valid for different address book references.