views:

128

answers:

2

Hi, I am trying to set an image for an contact that is store in my iPhone. I used follwing code snipt - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {

UIImage *image = [UIImage imageNamed:@"icon.png"];
NSData *data=UIImagePNGRepresentation(image);   
CFDataRef dr = CFDataCreate(NULL, [data bytes], [data length]);

ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate(); CFErrorRef erf=NULL;

if (ABPersonSetImageData(person, dr, nil))
{
    NSLog(@"successfully set image");
}
else
{
    NSLog(@"not successfully set image");
}

if( ABAddressBookSave(iPhoneAddressBook, &erf) )
{
    NSLog(@"save %s",erf);
}
else {
    NSLog(@"Not success");
}



CFRelease(dr);

CFRelease(iPhoneAddressBook); [self dismissModalViewControllerAnimated:YES];

return NO;

}

every thing is working but the image of the contact does not change. plz help!

+1  A: 

you are calling:

if (ABPersonSetImageData(person, dr, nil))

which is passing 'nil' as the image data..

Mobs
You're wrong - the function looks like this: `bool ABPersonSetImageData (ABPersonRef person, CFDataRef imageData);`
Adam Woś
whoops, I am wrong!
Mobs
+1  A: 

Take a look at this thread. This is the same code!

Regarding the merits: are you sure the reference to person you have passed to your method is valid in the context of the address book? I would try to use a function such as ABAddressBookGetPersonWithRecordID first, instead of passing ABPersonRefs around and expecting them to be valid for different address book references.

Seemed to help the author there :)

BTW, this line

CFDataRef dr = CFDataCreate(NULL, [data bytes], [data length]);

is not needed - NSData * and CFDataRef are interchangeable:

NSData is “toll-free bridged” with its Core Foundation counterpart, CFData Reference. This means that the Core Foundation type is interchangeable in function or method calls with the bridged Foundation object. Therefore, in a method where you see an NSData * parameter, you can pass a CFDataRef, and in a function where you see a CFDataRef parameter, you can pass an NSData instance (you cast one type to the other to suppress compiler warnings). This also applies to your concrete subclasses of NSData. See Interchangeable Data Types for more information on toll-free bridging.

(from NSData documentation).

Adam Woś