设置联系人的通讯簿图像似乎不起作用 [英] Setting Address Book image for a contact doesn't seem to work

查看:162
本文介绍了设置联系人的通讯簿图像似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码设置联系人图片。我没有看到任何错误,并且图像没有保存到通讯录中的联系人条目。请帮忙!我必须做错事...

I'm trying to set the contact image with the code below. I am not seeing any errors, and the image is not saved to the contact entry in the address book. Please help! I must be doing something wrong...

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
[abcontroller setDisplayedPerson:person];

UIImage *im = [UIImage imageNamed:@"image1.jpg"];
NSData *dataRef = UIImagePNGRepresentation(im);
CFErrorRef error;
ABAddressBookRef addressBook = ABAddressBookCreate(); 

NSLog(@"Error:",error);
    if (ABPersonSetImageData(person, (CFDataRef)dataRef, &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");
        }


    }
    else {
        NSLog(@"Error saving contact photo %@", error);
    }
ABAddressBookSave(addressBook, &error);

[self dismissModalViewControllerAnimated:YES];    

return NO;
}

这是我的输出日志:

2010-01-17 21:58:35.465 Error:
2010-01-17 21:58:35.504 Set contact photo <ABPeoplePickerNavigationController: 0x19e9b0>
2010-01-17 21:58:43.497 No changes made to address book
2010-01-17 21:58:44.724 Saved

我不知道为什么错误对象被记录为ABPeoplePickerNavigationController对象?

I'm not sure why the error object is logging as an ABPeoplePickerNavigationController object?

推荐答案

关于我不知道为什么错误对象作为ABPeoplePickerNavigationController对象记录? - 因为您没有正确初始化错误这里:

Regarding I'm not sure why the error object is logging as an ABPeoplePickerNavigationController object? -- because you're not properly initialising the error here:

CFErrorRef error;

在此分配 nil ,否则将一个随机(或更准确地说,一些以前的内存)值,附带地指向一个 ABPeoplePickerNavigationController 对象。

Assign nil here, or it will have a random (or more precisely, some previous memory) value, incidentally pointing to an ABPeoplePickerNavigationController object.

关于优点:您确定您已经传递给您的方法的人员引用在地址簿的上下文中有效吗?我会尝试首先使用诸如 ABAddressBookGetPersonWithRecordID 的函数,而不是传递 ABPersonRef ,并期望它们有效对于不同的地址簿参考。

Regarding the merits: are you sure the reference to person you have passed to your method is valid in the context of the address book? I would try to use a function such as ABAddressBookGetPersonWithRecordID first, instead of passing ABPersonRefs around and expecting them to be valid for different address book references.

这篇关于设置联系人的通讯簿图像似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆