简体   繁体   English

设置联系人iOS的图片

[英]Set image for contact iOS

I am using ABPeoplePickerNavigationController for representation table of contacts. 我正在使用ABPeoplePickerNavigationController作为联系人的表示表。 By tapping contact I need to set new image for it. 通过点击联系人,我需要为其设置新图像。 I added code to delegate for changing person data, but can't change image. 我添加了代码以委派更改人的数据,但不能更改图像。 Any suggestions? 有什么建议么?

This is my code below: 这是我的代码如下:

 - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    NSData *dataRef = UIImagePNGRepresentation(self.theImage);
    CFDataRef cfdata = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);
    CFErrorRef error;

    ABPersonRemoveImageData(person, &error); // clean any image first from ref
    if (ABAddressBookSave(_addressBook, &error))
    {
        ABPersonSetImageData(person, cfdata, &error);
        ABAddressBookSave(_addressBook, &error);
    }
    CFRelease(cfdata);

    [self dismissViewControllerAnimated:YES completion:nil];

    return NO;
}

I downloaded sample from here: link 我从这里下载了示例: 链接

To check how it works you can download code and modify delegate below with my code. 要检查其工作原理,您可以下载代码并在下面用我的代码修改委托。

You need to save the changes via ABAddressBookSave() 您需要通过ABAddressBookSave()保存更改

Also, keep in mind if your contact does NOT already have an image, both the thumbnail and the full sized image will be added when you use ABPersonSetImageData . 另外,请记住,如果您的联系人还没有图像,则在使用ABPersonSetImageData时将同时添加缩略图和全尺寸图像。 However, if your contact has a full-sized image already, only the thumbnail will be set when you use ABPersonSetImageData . 但是,如果您的联系人已经具有完整尺寸的图像,则在使用ABPersonSetImageData会设置缩略图。

// this is not production level code. method call return values and errors
// need to be handled properly
ABPersonRemoveImageData(person, &error); // clean any image first from ref
if (ABAddressBookSave(addressBook, &error))
{
   ABPersonSetImageData(person, cfdata, &error);
   ABAddressBookSave(addressBook, &error)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM