我的应用程序中有一个地址簿,它是通过 json 从网站填充的。如果我在我的网站上添加新人员,该人员将在下次启动时添加到应用程序地址簿中。如果我在我的网站上更新一个人的名字,它会完美地工作..但如果我向现有的人添加新图像,它在重新加载后仍然显示来自 Apple 的旧默认图像。如果我编译它,就会出现图片。通讯录图片有缓存吗?
我尝试了以下方法:
BOOL imgBool = ABPersonHasImageData(aContact); -> false
和
ABPersonRemoveImageData(aContact, &anError); -> false because no picture in AB
我添加了这样的人物图片:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[[self.profsInSection objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] bildUrl]]]];
NSData *picData = UIImageJPEGRepresentation(image, 0.9f);
[...]
ABPersonSetImageData(aContact, (CFDataRef)picData, nil);
没有错误,它只是保存最后一张图片并忽略新图片...:( 有什么想法吗?
最佳答案
应用地址簿中的更改后,您是否忘记调用保存功能?
ABAddressBookSave(addrBook, nil);
更新:所以,只要您使用 ABUnknownPersonViewController,我就自己尝试了以下代码,并且它在模拟器和设备上都有效
ABRecordRef newPerson = ABPersonCreate();
ABRecordSetValue(newPerson, kABPersonLastNameProperty, @"Smith", nil);
UIImage *personImage = [UIImage imageNamed:@"image.jpg"];
NSData *dataRef = UIImageJPEGRepresentation(personImage, 0.9f);
CFErrorRef error = nil;
bool result = ABPersonSetImageData(newPerson, (CFDataRef)dataRef, &error);
ABUnknownPersonViewController *controller = [ABUnknownPersonViewController new];
//controller.addressBook = addressBook;
controller.allowsAddingToAddressBook = YES;
controller.displayedPerson = newPerson;
[self.navigationController pushViewController:controller animated:YES];
[controller release];
我在测试此代码时遇到的问题是图像名称输入错误,因此 UIImage
实例为零,NSData
为空。 ABPersonSetImageData
对此没有给出任何错误。因此,请仔细检查您的 UIImage
实例是否为零。
关于cocoa - ABPerson 使用旧图片数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8213399/