gpt4 book ai didi

iphone - 将联系人图像添加到地址簿 iPhone

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:21:07 29 4
gpt4 key购买 nike

我正在开发应用程序,当我从联系人选择器中选择任何联系人时,该应用程序会更改联系人的联系人图片。

这是我到目前为止实现的代码,

(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
CFErrorRef error=nil;
ABAddressBookRef addressBook = ABAddressBookCreate();
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"birthday.jpg" ofType:nil];
UIImage *img = [UIImage imageWithContentsOfFile:path1];
NSData *dataRef = UIImagePNGRepresentation(img);

CFDataRef cfDataRef = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);

if(ABPersonHasImageData(person))
{
ABPersonRemoveImageData(person, &error);
ABAddressBookSave(addressBook, &error);
}
if (ABPersonSetImageData(person,cfDataRef, &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");
}
}

CFRelease(cfDataRef);

// TODO: release peoplePicker (and refactor code to not have it global)
[self dismissModalViewControllerAnimated:YES];

return NO;

}

但是当我调试并查看 ABAddressBookHasUnsavedChanges 时,它返回 false 并且图像未设置为联系人。可能是什么原因?

我在shouldContinueAfterSelectingPerson中写了这个方法;我检查了图像,它不为空,ABPersonSetImageData 返回 TRUE

我已经更新了代码,已经检查了图像集,如果是,那么我将删除该图像并保存地址簿和 relased CFDataRef。

还是不行

Update:2 
I have edited the code , please look at the changes.

我已经用这个方法写了我所有的代码
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { } ,
所以肯定会有已经在地址簿中的人。但是我根据上面的代码尝试仍然返回 false ABAddressBookHasUnsavedChanges

最佳答案

你创建的数据是错误的——你应该这样做。

UIImage *img = [UIImage imageWithContentsOfFile:path1];
NSData *dataRef = UIImagePNGRepresentation(img);
CFDataRef cfDataRef = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);

也尝试在开始的某处设置一个断点,并检查是否有内容不为 nil 或没有错误的值。

更新

我已经在实际的 Xcode 中尝试过,只是为了测试。您的代码告诉您地址簿没有未保存更改的原因是因为您尚未在其中添加记录 - 否则您正在为尚未添加到地址簿的联系人设置图像(即不是一个问题,你可以毫无问题地这样做,但联系人本身不在地址簿中,直到你保存它)。所以对你来说神奇的代码行可能是这样的:

ABAddressBookAddRecord(addressBook, person, &error);

这是我一直在使用的完整代码。

   CFErrorRef error = nil;
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef person = ABPersonCreate();
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"sky_main" ofType:@"jpg"];

UIImage *img = [UIImage imageWithContentsOfFile:path1];
NSData *dataRef = UIImagePNGRepresentation(img);
CFDataRef cfDataRef = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);

if (ABPersonHasImageData(person)) {
ABPersonRemoveImageData(person, &error);
ABAddressBookSave(addressBook, &error);
}
ABAddressBookAddRecord(addressBook, person, &error);

if (ABPersonSetImageData(person, cfDataRef, &error)) {
if (ABAddressBookHasUnsavedChanges(addressBook)) {
NSLog(@"has unsaved changes");
} else {
NSLog(@"nothing to save");
}
if (ABAddressBookSave(addressBook, &error)) {
NSLog(@"saved");
} else {
NSLog(@"not saved");
}
}


ABUnknownPersonViewController *ctr = [[ABUnknownPersonViewController alloc] init];
ctr.unknownPersonViewDelegate = nil;
ctr.displayedPerson = person;
ctr.allowsAddingToAddressBook = YES;
ctr.allowsActions = YES;
ctr.hidesBottomBarWhenPushed = YES;

// [[[CCDirector sharedDirector] view] addSubview:ctr.view];
CFRelease(person);
CFRelease(addressBook);

我也在用AddressBookUI,但你不一定要用(我只是想看看未保存的联系人的变化)。

更新 2

好的,刚刚创建了一个新项目只是为了尝试。看起来问题实际上出在 ABAddressBookRef 上。您必须从委托(delegate)方法的 peoplePicker 参数中获取它。由于它是委托(delegate)方法,因此您不必添加记录,因为它已经存在。这是必须工作的代码(2 分钟前试过)。让我知道:)

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {

CFErrorRef error = nil;
ABAddressBookRef addressBook = peoplePicker.addressBook;
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"cat" ofType:@"jpg"];

UIImage *img = [UIImage imageWithContentsOfFile:path1];
NSData *dataRef = UIImagePNGRepresentation(img);
CFDataRef cfDataRef = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);

if (ABPersonHasImageData(person)) {
ABPersonRemoveImageData(person, &error);
ABAddressBookSave(addressBook, &error);
}

if (ABPersonSetImageData(person, cfDataRef, &error)) {
if (ABAddressBookHasUnsavedChanges(addressBook)) {
NSLog(@"has unsaved changes");
} else {
NSLog(@"nothing to save");
}
if (ABAddressBookSave(addressBook, &error)) {
NSLog(@"saved");
} else {
NSLog(@"not saved");
}
}

CFRelease(addressBook);

return YES;
}

顺便说一句,因为它是委托(delegate)方法不要释放这个人,否则它会可怕地崩溃 - 开个玩笑,它会崩溃,因为它试图访问不存在的内存。

关于iphone - 将联系人图像添加到地址簿 iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17741599/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com