Setting the address book image for a contact does not work

I am trying to establish a contact image using the code below. I do not see any errors, and the image is not saved in the contact entry in the address book. Please help! I have to do 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 registered as an ABPeoplePickerNavigationController object?

+3
source share
4 answers

As for I'm not sure why the error object is registered as an ABPeoplePickerNavigationController object? - because you are not initializing correctly errorhere:

CFErrorRef error;

nil , (, , ), , ABPeoplePickerNavigationController.

: , person, , ? ABAddressBookGetPersonWithRecordID, ABPersonRef , .

+1

. :

ABAddressBookRef addressBook = ABAddressBookCreate(); 

ABAddressBookRef addressBook = [peoplePicker addressBook];

" ", "" .

+5

() , , , !

, Apple SDK, .

, . .

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    @try {
        CFErrorRef cfError = nil;

        if (ABPersonHasImageData(person)) {
            NSLog(@"%s has an image", _cmd);

            //theAlert = [[UIAlertView alloc] initWithTitle:@"Contact already has an image" message:@"Are you sure you want to overwrite it?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Overwrite"];
            //[theAlert show];
            //[theAlert release];
            //Note: We do not yet handle the answer to this question
        }

        // Just a sanity check

        if ((nil == [imageView image])
            || (nil == UIImagePNGRepresentation([imageView image]))) {
            NSLog(@"%s NIL STUFF", _cmd);
        }

        // Not sure I even need to create and save an address book
        // Cf. https://devforums.apple.com/message/27512#27512
        ABAddressBookRef libroDirec = ABAddressBookCreate();
        ABPersonSetImageData(person, (CFDataRef) (UIImageJPEGRepresentation([imageView image], 1.0f)), &cfError);
        ABAddressBookAddRecord(libroDirec, person, &cfError);
        if (ABAddressBookSave(libroDirec, nil)) {
            NSLog(@"%s saved successfuly", _cmd);
        } else {
            NSLog(@"%s something bad happen while saving", _cmd);
        }
        CFRelease(libroDirec);
    }
    @catch (NSException * e) {

    }
    @finally {

    }
    // TODO: release peoplePicker (and refactor code to not have it global)
    [self dismissModalViewControllerAnimated:YES];

    return NO;

}
+2

, . 2 .

ABAddressBookRef libroDirec = [peoplePicker addressBook];//don't create
        ABPersonSetImageData(person, (CFDataRef)UIImagePNGRepresentation(display.image), &cfError);
        ABAddressBookAddRecord(libroDirec, person, &cfError);

        if (ABAddressBookSave(libroDirec, nil)) {
            NSLog(@"\n%s saved successfuly", _cmd);
        } else {
            NSLog(@"\n%s something bad happen while saving", _cmd);
        }
        //CFRelease(libroDirec);

, .

+2

Source: https://habr.com/ru/post/1728853/


All Articles