3

我正在开发一个与 AddressBook 联系人一起使用的 iPhone 应用程序。我正在尝试从通讯簿中获取图像并编辑然后保存该图像。

但我不知道如何更新和保存该图像。如果有人知道这一点,请给我一个解决方案。

提前谢谢。

这是.h文件

ABAddressBookRef addressBook;
IBOutlet UILabel *lblName;
IBOutlet UIImageView *imgGet;
UIImage *imgContact;
NSString* name;

.m 文件

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
    name = [(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) autorelease];
    lblName.text = name;
    if(ABPersonHasImageData(person)) {
        CFDataRef imageData = ABPersonCopyImageData(person);
        imgContact = [UIImage imageWithData:(NSData *)imageData];
        imgGet.image = imgContact;
        CFRelease(imageData);
    } else {
        imgGet.image = [UIImage imageNamed:@"ABPicturePerson@2x.png"];
    }

    [self.navigationController dismissViewControllerAnimated:(YES) completion:nil];

return NO;
}

在这里,从上述方法获取图像和名称后,我打开图库并选择图像,然后单击下面的方法按钮,但图像未保存或更新在地址簿中

-(IBAction)btnSaveClicked {
ABRecordRef person = ABPersonCreate();
    NSData *dataRef = UIImagePNGRepresentation(imgGet.image);
    CFDataRef cfdata = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);
 ABPersonSetImageData(person, (CFDataRef)dataRef, nil);
    CFErrorRef error;

    ABPersonRemoveImageData(person, &error); // <-- clean any image first from ref
    //ABAddressBookSave(addressBook, &error);

    ABPersonSetImageData(person, cfdata, &error);

    ABAddressBookSave(addressBook, &error);
    CFRelease(cfdata);

}

4

2 回答 2

3

此链接提示我如何更新您的图像..

http://davidbits.blogspot.in/2010/01/iphone-update-addressbook-contact.html

在 .h 文件中声明 1 个对象

NSInteger recordID;

然后打开联系人列表并获取联系人,我们使用此方法

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController     *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
   recordID  =  ABRecordGetRecordID(person);
    name = [(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) autorelease];
    lblName.text = name;
    if(ABPersonHasImageData(person)) {
    CFDataRef imageData = ABPersonCopyImageData(person);
    your uiimage = [UIImage imageWithData:(NSData *)imageData];
    you uiimageview.image = your uiimage;
    CFRelease(imageData);
   } else {
    you uiimageview.image = [UIImage imageNamed:@"ABPicturePerson@2x.png"];
   }

    [self.navigationController dismissViewControllerAnimated:(YES) completion:nil];
    //[self.navigationController dismissModalViewControllerAnimated:YES];
return NO;
}

然后为保存详细信息或更新详细信息编写操作

-(IBAction)btnSaveClicked
{
CFErrorRef *aberror = NULL;    
ABRecordRef aRecord = ABAddressBookGetPersonWithRecordID(addressBook, recordID);
if (aRecord) {
    NSData *dataRef = UIImagePNGRepresentation(your uiimage);
    ABPersonSetImageData(aRecord, (CFDataRef)dataRef, nil);
}
ABAddressBookSave(addressBook, aberror);
//BOOL didAdd = ABAddressBookSave(addressBook, nil);
CFRelease(addressBook);
}

确定你会得到结果..

于 2013-04-20T12:39:54.100 回答
2

ABRecordRef person要更新时使用:

首先create ABAddressBookRef

// Request authorization to Address Book
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

现在check personimage

if(ABPersonHasImageData(person)) 
{
   //remove image as we want to update to new image
   NSError *anError;
   if(ABPersonRemoveImageData(person, &anError))
      NSLog(@"removed");
   else
      NSLog(@"%@",[anError description]);
}

Update

 UIImage *image = image here;
 NSData *picData = UIImageJPEGRepresentation(image, 0.9f);
 ABPersonSetImageData(person, (CFDataRef)picData, nil);

致电saving function

 ABAddressBookSave(addressBookRef, nil);
于 2013-04-19T06:03:45.110 回答