我正在开发应用程序,当我从联系人选择器中选择任何联系人时,它会更改联系人的联系人图片。
这是我到目前为止实现的代码,
(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
CFErrorRef error=nil;
ABAddressBookRef addressBook = ABAddressBookCreate();
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"birthday.jpg" ofType:nil];
UIImage *img = [UIImage imageWithContentsOfFile:path1];
NSData *dataRef = UIImagePNGRepresentation(img);
CFDataRef cfDataRef = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);
if(ABPersonHasImageData(person))
{
ABPersonRemoveImageData(person, &error);
ABAddressBookSave(addressBook, &error);
}
if (ABPersonSetImageData(person,cfDataRef, &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");
}
}
CFRelease(cfDataRef);
// TODO: release peoplePicker (and refactor code to not have it global)
[self dismissModalViewControllerAnimated:YES];
return NO;
}
但是当我调试并查看ABAddressBookHasUnsavedChanges
它返回 false 并且图像未设置为联系人时。可能是什么原因?
我在shouldContinueAfterSelectingPerson
;中写了这个方法 我检查了图像,它不是 null 并且ABPersonSetImageData
正在返回TRUE
。
我已经更新了代码,已经检查了图像集,如果是,那么我将删除该图像并保存保存地址簿并重新发布 CFDataRef。
但它仍然无法正常工作
Update:2
I have edited the code , please look at the changes.
我已经在这个方法中编写了所有代码
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { } ,
所以肯定会有人已经在地址簿中。但是我尝试根据上面的代码仍然认为 ABAddressBookHasUnsavedChanges 返回 false