修改iphone联系人头像

时间:2023-03-09 23:14:42
修改iphone联系人头像

如何通过代码来修改iphone联系人头像

使用如下代码:

UIImage *image=[UIImage imageNamed:@"star_full.png"];

NSData *data=UIImagePNGRepresentation(image);

ABPersonRemoveImageData(person, NULL);

CFDataRef cfData=CFDataCreate(NULL, [data bytes], [data length]);

ABPersonSetImageData(person, cfData, nil);

但是只能临时修改,这里需要一次保存的过程

UIImage *image=[UIImage imageNamed:@"star_full.png"];

ABAddressBookRef addressbook = ABAddressBookCreate();

NSData *data=UIImagePNGRepresentation(image);

ABPersonRemoveImageData(person, NULL);

CFDataRef cfData=CFDataCreate(NULL, [data bytes], [data length]);

ABPersonSetImageData(person, cfData, nil);

ABAddressBookAddRecord(addressbook, person, nil);

ABAddressBookSave(addressbook, nil);

上面的代码可以添加联系人头像,但是未能实现修改联系人头像功能,这里还缺少一步

UIImage *image=[UIImage imageNamed:@"star_full.png"];

ABAddressBookRef addressbook = ABAddressBookCreate();

NSData *data=UIImagePNGRepresentation(image);

ABPersonRemoveImageData(person, NULL);

ABAddressBookAddRecord(addressbook, person, nil);

ABAddressBookSave(addressbook, nil);

CFDataRef cfData=CFDataCreate(NULL, [data bytes], [data length]);

ABPersonSetImageData(person, cfData, nil);

ABAddressBookAddRecord(addressbook, person, nil);

ABAddressBookSave(addressbook, nil);

在删除联系人头像后需要保存一次才可以成功

这样就完美的解决了