Addressbook Save Image for contacts programmatically

风流意气都作罢 提交于 2019-12-08 04:30:22

问题


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


回答1:


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);


来源:https://stackoverflow.com/questions/5744017/addressbook-save-image-for-contacts-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!