更改联系人的图像时

时间:2011-11-16 23:07:42

标签: objective-c ios4 addressbook

我正在尝试更改peoplePickerNavigationController中所选联系人的图片,但是当我选择联系人时,不会更改图片并且不会显示任何错误。

嗯,据我所知,没关系!有什么东西丢失了?

这是代码:

ABAddressBookRef aBook = ABAddressBookCreate();

UIImage *img = [UIImage imageNamed:@"90x90.png"];
NSData *dataRef = UIImagePNGRepresentation(img);
CFDataRef cfdata = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);
CFErrorRef error;

if(ABPersonRemoveImageData(person, &error)){
    NSLog(@"OK");
}
if(ABAddressBookSave(aBook, &error)){
    NSLog(@"OK");
}

if(ABPersonSetImageData(person, cfdata, &error)){
    NSLog(@"OK");
}
if(ABAddressBookSave(aBook, &error)){
    NSLog(@"OK");
}

CFRelease(cfdata);

[self dismissModalViewControllerAnimated:YES];
return NO;

记住上面的代码在BOOL中:

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

谢谢!

1 个答案:

答案 0 :(得分:0)

管理解决,对于将来有同样问题的人,只需使用ABRecordGetRecordIDABAddressBookGetPersonWithRecordID,然后按照以下代码:

ABAddressBookRef aBook = ABAddressBookCreate();

NSNumber *recordId = [NSNumber numberWithInteger:ABRecordGetRecordID(person)];
ABRecordRef pID = ABAddressBookGetPersonWithRecordID(aBook,recordId.integerValue);

UIImage *img = [UIImage imageNamed:@"90x90.png"];
NSData *dataRef = UIImagePNGRepresentation(img);
CFDataRef cfdata = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);
CFErrorRef error;

ABPersonRemoveImageData(pID, &error);
ABAddressBookSave(aBook, &error);

ABPersonSetImageData(pID, cfdata, &error);
ABAddressBookSave(aBook, &error);

CFRelease(cfdata);

[self dismissModalViewControllerAnimated:YES];
return NO;

谢谢!