我正在尝试更改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 { }
谢谢!
答案 0 :(得分:0)
管理解决,对于将来有同样问题的人,只需使用ABRecordGetRecordID
和ABAddressBookGetPersonWithRecordID
,然后按照以下代码:
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;
谢谢!