CNContact.imageData not working

I set imageData to CNContact and save, but when I call the person I save, headImage does not 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];
+4
source share
1 answer

You need to create a select query 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];
    }];
}
0
source

Source: https://habr.com/ru/post/1624725/


All Articles