我正在尝试使用下面的代码设置联系人图像。我没有看到任何错误,图像也没有保存到地址簿中的联系人条目中。请帮忙!我一定做错了什么...
- (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 对象?
最佳答案
关于我不确定为什么错误对象记录为 ABPeoplePickerNavigationController 对象? -- 因为您没有正确初始化 error
此处:
CFErrorRef error;
在此处分配
nil
,否则它将具有随机(或更准确地说,某些先前的内存)值,顺便指向一个 ABPeoplePickerNavigationController
对象。关于优点:您确定传递给方法的
person
引用在地址簿的上下文中有效吗?我会尝试首先使用诸如 ABAddressBookGetPersonWithRecordID
之类的函数,而不是传递 ABPersonRef
s 并期望它们对不同的地址簿引用有效。关于iphone - 为联系人设置地址簿图像似乎不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2082845/