我正在使用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
要检查其工作原理,您可以使用我的代码下载代码并修改下面的代理。
答案 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)
}