如何使用CNContactVCardSerialization dataWithContacts:方法获取联系人图像的VCF数据? [英] How to get VCF data with contact images using CNContactVCardSerialization dataWithContacts: method?

查看:177
本文介绍了如何使用CNContactVCardSerialization dataWithContacts:方法获取联系人图像的VCF数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CNContacts和CNContactUI框架并通过此选择联系人

I'm using CNContacts and CNContactUI framework and picking a contact via this

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即将推出在日志中。但当我试图通过

This contact object have contact.imageData and coming in logs. But when I tried to cross check this data by

NSArray *contactList = [NSArray arrayWithArray:[CNContactVCardSerialization contactsWithData:data error:nil]];
CNContact *contactObject = [contactList objectAtIndex:0];

这是空的:

//contactObject.imageData  

为什么我得到这个null签入联系人时联系人是否有图像?

Why am I getting this null and this contact has image when check in contacts?

推荐答案

作为一种解决方法,您可以在VCard中创建PHOTO字段。

As a workaround you can create PHOTO field inside of VCard.

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如下所示:

For some reasons CNContactVCardSerialization does not use any photo of contact. VCard after serialization is looks like:

BEGIN:VCARD
VERSION:3.0
PRODID:-//Apple Inc.//iPhone OS 9.3.2//EN
N:Contact;Test;;;
FN: Test  Contact 
END:VCARD

插入PHOTO字段后VCard你会得到

After insertion the PHOTO field inside VCard you will get

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中的联系人看起来很好

After this insertion contact will looks fine in CNContactViewController

这篇关于如何使用CNContactVCardSerialization dataWithContacts:方法获取联系人图像的VCF数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆