Setting Address Book image for a contact doesn't seem to work

蹲街弑〆低调 提交于 2019-11-29 02:39:54

Regarding I'm not sure why the error object is logging as an ABPeoplePickerNavigationController object? -- because you're not properly initialising the error here:

CFErrorRef error;

Assign nil here, or it will have a random (or more precisely, some previous memory) value, incidentally pointing to an ABPeoplePickerNavigationController 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 as ABAddressBookGetPersonWithRecordID first, instead of passing ABPersonRefs around and expecting them to be valid for different address book references.

All these answer didn't work. You should simply change your line of code from:

ABAddressBookRef addressBook = ABAddressBookCreate(); 

to:

ABAddressBookRef addressBook = [peoplePicker addressBook];

This way you get the reference to the "address book", "person" belongs to.

Abu Bashar

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.

- (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;

}

I checked your code and it doesn't work as it should. So I made 2 changes.

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

Now it works as it supposed to.

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