4

I set the imageData in CNContact and save,but when I call the person who I save the headImage doesn't work.

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];
F.Rao
  • 41
  • 2

1 Answers1

0

You need to create a fetch request if you need image data.

    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];
    }];
}
Marek H
  • 5,173
  • 3
  • 31
  • 42