Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

How to add image to contact

vishalvishal Posts: 76Registered Users
edited February 2010 in iPhone SDK Development
Hi,

I am trying to add image to a contact but could not add it.
Following is my code can anyone help me here?
CGImageRef sourceImageRef = [currentImage CGImage];  
CFDataRef photoDataRef = CGDataProviderCopyData(CGImageGetDataProvider(sourceImageRef));

ABRecordRef newPerson = ABPersonCreate();
ABPersonSetImageData(newPerson, photoDataRef, nil);
ABRecordSetValue(newPerson, kABPersonFirstNameProperty, @"John", nil);
ABRecordSetValue(newPerson, kABPersonLastNameProperty, @"Doe", nil);

ABNewPersonViewController *ab = [[ABNewPersonViewController alloc] init];
[ab setNewPersonViewDelegate:self];
ab.displayedPerson = newPerson;

navController = [[UINavigationController alloc] initWithRootViewController:ab];
[ab release];
	
[self presentModalViewController:navController animated:YES];
[navController release];

I can add single record without photo, but when tried to add with photo record gets added but photo is not displayed.

I am using function "ABPersonSetImageData" to add the image, what format it requires?

Vishal N
Post edited by vishal on

Replies

  • vishalvishal Posts: 76Registered Users
    edited April 2009
    vishal wrote: »
    Hi,

    I am trying to add image to a contact but could not add it.
    Following is my code can anyone help me here?
    CGImageRef sourceImageRef = [currentImage CGImage];  
    CFDataRef photoDataRef = CGDataProviderCopyData(CGImageGetDataProvider(sourceImageRef));
    
    ABRecordRef newPerson = ABPersonCreate();
    ABPersonSetImageData(newPerson, photoDataRef, nil);
    ABRecordSetValue(newPerson, kABPersonFirstNameProperty, @"John", nil);
    ABRecordSetValue(newPerson, kABPersonLastNameProperty, @"Doe", nil);
    
    ABNewPersonViewController *ab = [[ABNewPersonViewController alloc] init];
    [ab setNewPersonViewDelegate:self];
    ab.displayedPerson = newPerson;
    
    navController = [[UINavigationController alloc] initWithRootViewController:ab];
    [ab release];
    	
    [self presentModalViewController:navController animated:YES];
    [navController release];
    

    I can add single record without photo, but when tried to add with photo record gets added but photo is not displayed.

    I am using function "ABPersonSetImageData" to add the image, what format it requires?

    Vishal N

    Hi,

    I have got the solution for this.

    We need to create the PNG representation of the image.
    NSData * dataRef = UIImagePNGRepresentation(newImage);
    
    and add this to the records
    
    ABPersonSetImageData(newPerson, (CFDataRef)dataRef, nil);
    

    It adds the image to the record.

    Vishal N
Sign In or Register to comment.