Search code examples
iphoneios4iphone-sdk-3.0addressbookabaddressbook

Addressbook Save Image for contacts programmatically


I am creating an addressbook application in that all the data are stored to server and when User will ask then server will restore all the contacts to iPhone.

My problem is how to send Image to server as well as how I will be able to restore the contact Image , I came to know that my server will provide me an Image in base64 encryption format.

So can any help me how to perform image saving and retrieving for Addressbook Programmatically


Solution

  • You need to convert the base64 image in nsdata and then you can set it to a contact, check "ABPersonSetImageData" in following code.

    ABRecordSetValue(newPerson, kABPersonOrganizationProperty,data.name, &error);
    
            ABMutableMultiValueRef multiURL = ABMultiValueCreateMutable(kABMultiStringPropertyType);
            ABMultiValueAddValueAndLabel(multiURL, homePageURL, kABPersonHomePageLabel, NULL);
            ABRecordSetValue(newPerson, kABPersonURLProperty, multiURL,&error);
            CFRelease(multiURL);
    
            ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
            ABMultiValueAddValueAndLabel(multiPhone, contactNumber, kABPersonPhoneMobileLabel, NULL);
            ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,&error);
            CFRelease(multiPhone);
    
            ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
            ABMultiValueAddValueAndLabel(multiEmail, emailIDs, kABHomeLabel, NULL);
            ABRecordSetValue(newPerson, kABPersonEmailProperty, multiEmail, &error);
            CFRelease(multiEmail);
    
            ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
            NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
            [addressDictionary setObject:toAddress forKey:(NSString *) kABPersonAddressStreetKey];
            [addressDictionary setObject:@"Amsterdam" forKey:(NSString *) kABPersonAddressCityKey];
            [addressDictionary setObject:@"Amsterdam" forKey:(NSString *) kABPersonAddressStateKey];
            [addressDictionary setObject:@"00000" forKey:(NSString *) kABPersonAddressZIPKey];
            [addressDictionary setObject:@"Netharland" forKey:(NSString *) kABPersonAddressCountryKey];
            ABMultiValueAddValueAndLabel(multiAddress, addressDictionary, kABHomeLabel, NULL);
            ABRecordSetValue(newPerson, kABPersonAddressProperty, multiAddress,&error);
            CFRelease(multiAddress);
    
            NSData *data1 = UIImagePNGRepresentation([UIImage imageNamed:data.titleImg]);
            ABPersonSetImageData(newPerson, data1, &error);
    
    
            ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
            ABAddressBookSave(iPhoneAddressBook, &error);