Reputation: 5388
I am working on an iPhone application which works with AddressBook contacts. I am trying to fetch the image from address book and edit then save that image.
But I don't know how to update and save that image. Please give me a solution if anybody knows about this.
Thanx in advance.
Here's the .h file
ABAddressBookRef addressBook;
IBOutlet UILabel *lblName;
IBOutlet UIImageView *imgGet;
UIImage *imgContact;
NSString* name;
The .m file
- (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:@"[email protected]"];
}
[self.navigationController dismissViewControllerAnimated:(YES) completion:nil];
return NO;
}
Here After fetching image and name from above method then i open gallery and select an image then click on below method button but image not saved or update in address book
-(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);
}
Upvotes: 3
Views: 945
Reputation: 5388
This link is hint for me that how to update your image..
http://davidbits.blogspot.in/2010/01/iphone-update-addressbook-contact.html
declare 1 object in .h file
NSInteger recordID;
then open contact list and for fetching the contact we use this method
- (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:@"[email protected]"];
}
[self.navigationController dismissViewControllerAnimated:(YES) completion:nil];
//[self.navigationController dismissModalViewControllerAnimated:YES];
return NO;
}
then write action for save detail or update detail
-(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);
}
sure you will get result..
Upvotes: 3
Reputation: 38259
Use ABRecordRef person
when you want to update:
Firstly create
ABAddressBookRef
:
// Request authorization to Address Book
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
Now check
person
has image
:
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);
call saving function
:
ABAddressBookSave(addressBookRef, nil);
Upvotes: 2