设置联系iOS的图像

时间:2013-09-16 15:57:36

标签: ios abaddressbook abpeoplepickerview

我正在使用ABPeoplePickerNavigationController作为联系人的表示表。通过点击联系我需要为它设置新图像。我添加了代码来代理更改人员数据,但无法更改图像。有什么建议吗?

这是我的代码:

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

我从这里下载了样本:link

要检查其工作原理,您可以使用我的代码下载代码并修改下面的代理。

1 个答案:

答案 0 :(得分:1)

您需要通过ABAddressBookSave()

保存更改

另外,请记住,如果您的联系人还没有图像,则在使用ABPersonSetImageData时,将添加缩略图和完整尺寸的图像。但是,如果您的联系人已经拥有完整尺寸的图片,那么当您使用ABPersonSetImageData时,将设置缩略图。

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