PDA

View Full Version : Save image to address book




asifpv
Apr 21, 2009, 07:51 AM
Hi all,


How to add an UIImage to address book contact from another application.

Thanx,
asif



robbieduncan
Apr 21, 2009, 08:24 AM
Please take the time to read the documentation before posting questions. It's pretty obvious (it took me all of 20 seconds to find this) that this method (http://developer.apple.com/iphone/library/documentation/AddressBook/Reference/ABPersonRef_iPhoneOS/Reference/reference.html#//apple_ref/c/func/ABPersonSetImageData) is what you need to execute. The rest of the documentation will tell you how to get a reference to the correct ABPersonRef object.

asifpv
Apr 22, 2009, 03:20 AM
Hi robbieduncan,

Thanx for your reply, i did in my app like the following , but its not working..

CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(empImage.CGImage));

ABPersonSetImageData(person, imageData, nil);

ABAddressBookAddRecord(addrBook, person, nil);

ABAddressBookSave(addrBook, nil);


Please help me...


Thanks,
Asif

robbieduncan
Apr 22, 2009, 03:24 AM
Instead of passing nil for the error arguments why not pass and check for errors like you are supposed to?

satyamtyagi
Nov 18, 2009, 11:47 PM
UIImage *personImage;
personImage = [UIImage imageNamed:@"bl.png"];
NSData *dataRef = UIImagePNGRepresentation(personImage);
CFDataRef dr = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);

ABPersonSetImageData(newPerson, dr, &error);
if (error != NULL) {
NSLog(@"AB Person Set Image Failed");
}

ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
ABAddressBookSave(iPhoneAddressBook, &error);
if (error != NULL) {
NSLog(@"Address Book Save Failed");
}
CFRelease(dr);
CFRelease(newPerson);
CFRelease(iPhoneAddressBook);

North Bronson
Nov 19, 2009, 12:31 PM
NSData is also toll-free bridged with CFData, so you should be able to type-cast your NSData object as a CFData one and pass it in to "ABPersonSetImageData". This can save you a step or two.

NSData *data = UIImagePNGRepresentation(newImage);

ABPersonSetImageData(newPerson, (CFData *)data, &error);