我正在使用CNContacts和CNContactUI框架并通过此选择联系人
CNContactPickerViewController *contactPicker = [CNContactPickerViewController new]; contactPicker.delegate = self; [self presentViewController:contactPicker animated:YES completion:nil];
和
-(void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact { NSArray *array = [[NSArray alloc] initWithObjects:contact,nil]; NSError *error; NSData *data = [CNContactVCardSerialization dataWithContacts:array error:&error]; NSLog(@"ERROR_IF_ANY :: %@",error.description); }
此联系对象具有contact.imageData和日志.但当我试图通过交叉检查这些数据
NSArray *contactList = [NSArray arrayWithArray:[CNContactVCardSerialization contactsWithData:data error:nil]]; CNContact *contactObject = [contactList objectAtIndex:0];
这是空的:
//contactObject.imageData
为什么我收到此null并且此联系人在签入联系人时有图像?
解决方法
作为一种解决方法,您可以在VCard中创建PHOTO字段.
NSError* error = nil; NSData* vCardData = [CNContactVCardSerialization dataWithContacts:@[contact] error:&error]; NSString* vcString = [[NSString alloc] initWithData:vCardData encoding:NSUTF8StringEncoding]; NSString* base64Image = contact.imageData.base64Encoding; NSString* vcardImageString = [[@"PHOTO;TYPE=JPEG;ENCODING=BASE64:" stringByAppendingString:base64Image] stringByAppendingString:@"\n"]; vcString = [vcString stringByReplacingOccurrencesOfString:@"END:VCARD" withString:[vcardImageString stringByAppendingString:@"END:VCARD"]]; vCardData = [vcString dataUsingEncoding:NSUTF8StringEncoding];
由于某些原因,CNContactVCardSerialization不使用任何联系人照片.序列化后的VCard看起来像:
BEGIN:VCARD VERSION:3.0 PRODID:-//Apple Inc.//iPhone OS 9.3.2//EN N:Contact;Test;;; FN: Test Contact END:VCARD
在VCard中插入PHOTO字段后,您将获得
BEGIN:VCARD VERSION:3.0 PRODID:-//Apple Inc.//iPhone OS 9.3.2//EN N:Contact;Test;;; FN: Test Contact PHOTO;TYPE=JPEG;ENCODING=BASE64:<photo base64 string> END:VCARD
在此插入之后,联系人在CNContactViewController中看起来很好