ABPersonSetImageData not working

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!


you are calling:

if (ABPersonSetImageData(person, dr, nil))

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


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 ABPersonRef s 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).

链接地址: http://www.djcxy.com/p/51788.html

上一篇: CompletionHandler的外部数据无法使用

下一篇: ABPersonSetImageData不起作用