3

我正在尝试使用下面的代码设置联系人图像。我没有看到任何错误,并且图像未保存到地址簿中的联系人条目中。请帮忙!我一定做错了什么...

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

这是我的输出日志:

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

我不确定为什么错误对象记录为 ABPeoplePickerNavigationController 对象?

4

4 回答 4

5

所有这些答案都不起作用。你应该简单地改变你的代码行:

ABAddressBookRef addressBook = ABAddressBookCreate(); 

到:

ABAddressBookRef addressBook = [peoplePicker addressBook];

这样你就得到了对“通讯录”、“人”所属的引用。

于 2011-05-07T14:51:11.460 回答
2

我也尝试使用设置当前选定人的图像(照片),但它显示按摩成功保存但实际上它不起作用!!!

在挖掘出 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;

}
于 2010-02-27T09:57:28.133 回答
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);

现在它按预期工作。

于 2011-12-28T12:43:17.573 回答
1

关于我不确定为什么错误对象记录为 ABPeoplePickerNavigationController 对象?- 因为你没有正确初始化error这里:

CFErrorRef error;

在这里赋值nil,否则它将有一个随机的(或更准确地说,一些以前的记忆)值,顺便指向一个ABPeoplePickerNavigationController对象。

关于优点:您确定对person您的方法的引用在地址簿的上下文中有效吗?我会尝试使用诸如ABAddressBookGetPersonWithRecordIDfirst 之类的函数,而不是传递ABPersonRefs 并期望它们对不同的地址簿引用有效。

于 2010-01-17T22:39:13.557 回答