4

我在 CNContact 中设置了 imageData 并保存,但是当我打电话给我保存 headImage 的人时不起作用。

CNMutableContact *contact = [[CNMutableContact alloc] init];
UIImage *logo = [UIImage imageNamed:@"Default"];
NSData *dataRef = UIImagePNGRepresentation(logo);
contact.imageData = dataRef;
contact.givenName = "123";
contact.phoneNumbers = phoneNums;
//添加联系人
[saveRequest addContact:contact toContainerWithIdentifier:nil];
[contactStore executeSaveRequest:saveRequest error:nil];
4

1 回答 1

0

如果需要图像数据,则需要创建获取请求。

    CNContactStore *contactStore = [[CNContactStore alloc] init];
    NSPredicate *predicate = [CNContact predicateForContactsWithIdentifiers:@[[contact identifier]]];
    CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[[CNContactVCardSerialization descriptorForRequiredKeys],
                                                                                          CNContactImageDataKey,
                                                                                          CNContactThumbnailImageDataKey
                                                                                          ]];
    [request setPredicate:predicate];
    NSError *error;
    NSMutableArray *mutArray = [NSMutableArray array];
    [contactStore enumerateContactsWithFetchRequest:request error:&error usingBlock:^(CNContact * _Nonnull fetchedContact, BOOL * _Nonnull stop) {
        [mutArray addObject:fetchedContact];
    }];
}
于 2018-01-04T11:06:33.473 回答