简体   繁体   中英

Unable to show contact thumbnail image in tableview while using contacts framework in objective c

I am using a UITableView to display the mobile contact details like fullname, phonenumbers and the profile image. Below is the code

- (void)viewDidLoad {
    [super viewDidLoad];
    [self fetchContactsandAuthorization];



    [_selectContactListTblView reloadData];
}


-(void)fetchContactsandAuthorization
{
    // Request authorization to Contacts
    CNContactStore *store = [[CNContactStore alloc] init];
    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted == YES)
        {
            //keys with fetching properties
            NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey];
            NSString *containerId = store.defaultContainerIdentifier;
            NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
            NSError *error;
            NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];

            NSLog(@"new %@",cnContacts);

            if (error)
            {
                NSLog(@"error fetching contacts %@", error);
            }
            else
            {
                NSString *phone;
                NSString *fullName;
                NSString *firstName;
                NSString *lastName;
                UIImage *profileImage;
                NSMutableArray *contactNumbersArray = [[NSMutableArray alloc]init];
                for (CNContact *contact in cnContacts) {
                    // copy data to my custom Contacts class.
                    firstName = contact.givenName;
                    lastName = contact.familyName;

                    if (lastName == nil) {
                        fullName=[NSString stringWithFormat:@"%@",firstName];
                    }else if (firstName == nil){
                        fullName=[NSString stringWithFormat:@"%@",lastName];
                    }
                    else{
                        fullName=[NSString stringWithFormat:@"%@ %@",firstName,lastName];
                    }

                    UIImage *image = [UIImage imageWithData:contact.imageData];

                    if (image != nil) {
                        profileImage = image;
                    }else{
                        profileImage = [UIImage imageNamed:@"person-icon.png"];
                    }


                    for (CNLabeledValue *label in contact.phoneNumbers) {
                        phone = [label.value stringValue];
                        if ([phone length] > 0) {
                            [contactNumbersArray addObject:phone];
                        }
                    }
                    NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys: fullName,@"fullName",profileImage,@"userImage",phone,@"PhoneNumbers", nil];
                    NSLog(@"persondictfull %@",personDict);
                    NSLog(@"persondictp %@",[personDict objectForKey:@"PhoneNumbers"]);
                    [contactFullNameArr addObject:[NSString stringWithFormat:@"%@",[personDict objectForKey:@"fullName"]]];
                    [contactPhoneNumberArr addObject:[NSString stringWithFormat:@"%@",[personDict objectForKey:@"PhoneNumbers"]]];
                    [contactImgPathArr addObject:[NSString stringWithFormat:@"%@",[personDict objectForKey:@"userImage"]]];
                    NSLog(@"contactImg - %@",contactImgPathArr);
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    [_selectContactListTblView reloadData];
                });
            }
        }
    }];
}

For reference this is what generated in the contactImg log

 contactImg -  (
        "<UIImage: 0x7b935760>, {196, 199}",
        "<UIImage: 0x7b77b1e0>, {196, 199}",
        "<UIImage: 0x7b6d48a0>, {196, 199}",
        "<UIImage: 0x7b6d4b40>, {196, 199}",
        "<UIImage: 0x7b87d8e0>, {196, 199}",
        "<UIImage: 0x7b87d950>, {3000, 2002}",
        "<UIImage: 0x7b77b730>, {196, 199}",
        "<UIImage: 0x7b77b9a0>, {196, 199}",
        "<UIImage: 0x7b6d4c90>, {196, 199}",
        "<UIImage: 0x7b87df10>, {196, 199}",
        "<UIImage: 0x7b6d54a0>, {196, 199}",
        "<UIImage: 0x7b9334c0>, {196, 199}"
    )




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SelectContactTableViewCell";

    SelectContactTableViewCell *cell = (SelectContactTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SelectContactTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.contactFullName.text = [contactFullNameArr objectAtIndex:indexPath.row];


  //tried by directly assigning the value but no use
    cell.contactProfileImage.image = [contactImgPathArr objectAtIndex:indexPath.row]; 



//if i assign any static image i am able to see the image
// cell.contactProfileImage.image = [UIImage imageNamed:@"person-icon.png"];
        cell.contactPhoneNumber.text = [contactPhoneNumberArr objectAtIndex:indexPath.row];

        return cell;
    }

screenshot for refernece where i am getting full name and phonenumber details but no thumbnail 在此处输入图片说明

I tried a lot but unable to figure out the problem. Any help will be really appreciated.

You have a few issues. The primary issue starts here:

[contactImgPathArr addObject:[NSString stringWithFormat:@"%@",[personDict objectForKey:@"userImage"]]];

personDict[@userImage"] is a UIImage . For some reason you create a string from the image's description method and store that useless string in your contactImgPathArr .

Later, you attempt to misuse UIImage imageNamed: by passing in the image's description.

So you need two changes. First, change:

[contactImgPathArr addObject:[NSString stringWithFormat:@"%@",[personDict objectForKey:@"userImage"]]];

to:

[contactImgPathArr addObject:[personDict objectForKey:@"userImage"]];

(even better is:)

[contactImgPathArr addObject:personDict[@"userImage"]];

Then change:

cell.contactProfileImage.image = [UIImage imageNamed:[contactImgPathArr objectAtIndex:indexPath.row]];

to:

cell.contactProfileImage.image = [contactImgPathArr objectAtIndex:indexPath.row];

(even better:)

cell.contactProfileImage.image = contactImgPathArr[indexPath.row];

BTW - your use of personDict is pointless as well.

This code:

NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys: fullName,@"fullName",profileImage,@"userImage",phone,@"PhoneNumbers", nil];
[contactFullNameArr addObject:[NSString stringWithFormat:@"%@",[personDict objectForKey:@"fullName"]]];
[contactPhoneNumberArr addObject:[NSString stringWithFormat:@"%@",[personDict objectForKey:@"PhoneNumbers"]]];
[contactImgPathArr addObject:[NSString stringWithFormat:@"%@",[personDict objectForKey:@"userImage"]]];

should simply be:

[contactFullNameArr addObject:fullName];
[contactPhoneNumberArr addObject:phone];
[contactImgPathArr addObject:profileImage];

Never use [NSString stringWithFormat:@"%@", someStringValue] when you can simply use someStringValue .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM