Save image to address book

Discussion in 'iPhone/iPad Programming' started by asifpv, Apr 21, 2009.

  1. macrumors newbie

    Joined:
    Apr 13, 2009
    #1
    Hi all,


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

    Thanx,
    asif
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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 is what you need to execute. The rest of the documentation will tell you how to get a reference to the correct ABPersonRef object.
     
  3. thread starter macrumors newbie

    Joined:
    Apr 13, 2009
    #3
    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
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Instead of passing nil for the error arguments why not pass and check for errors like you are supposed to?
     
  5. macrumors newbie

    Joined:
    Nov 18, 2009
    #5
    use UIImagePNGRepresentation

    UIImage *personImage;
    personImage = [UIImage imageNamed:mad:"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);
     
  6. macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #6
    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.

    Code:
    NSData *data = UIImagePNGRepresentation(newImage);
    
    ABPersonSetImageData(newPerson, (CFData *)data, &error);
     

Share This Page