我正在尝试使用下面的代码设置联系人图片。我没有看到任何错误,图像也没有保存到地址簿中的联系人条目。请帮忙!我一定做错了......
- (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对象记录的?
答案 0 :(得分:5)
所有这些答案都不起作用。 您只需更改以下代码行:
ABAddressBookRef addressBook = ABAddressBookCreate();
为:
ABAddressBookRef addressBook = [peoplePicker addressBook];
这样你就可以得到“地址簿”的引用,“人”属于。
答案 1 :(得分: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;
}
答案 2 :(得分: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);
现在它按预期工作。
答案 3 :(得分:1)
关于我不确定为什么错误对象是作为ABPeoplePickerNavigationController对象记录的? - 因为你没有在这里正确初始化error
:
CFErrorRef error;
在此处分配nil
,或者它会随机指向ABPeoplePickerNavigationController
对象(或更准确地说是某些先前的内存)值。
关于优点:您确定传递给您的方法的person
引用在地址簿的上下文中是否有效?我会尝试首先使用ABAddressBookGetPersonWithRecordID
之类的函数,而不是传递ABPersonRef
并期望它们对不同的地址簿引用有效。