ABPerson使用旧图片数据

时间:2011-11-21 14:29:59

标签: cocoa image

我的应用程序中有一个地址簿,由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);

没有错误,它只是保存最后一张图片而忽略了新图片... :(任何想法?

1 个答案:

答案 0 :(得分:1)

在地址簿中应用更改后,您是否忘记调用保存功能?

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实例为nil且NSData为空。 ABPersonSetImageData没有给出任何错误。因此,请仔细检查UIImage实例是否为零。