簡體   English   中英

在目標c中使用聯系人框架時,無法在表視圖中顯示聯系人縮略圖

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

我正在使用UITableView顯示移動聯系人詳細信息,例如全名,電話號碼和個人資料圖像。 下面是代碼

- (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];
                });
            }
        }
    }];
}

供參考,這是在contactImg日志中生成的內容

 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;
    }

我獲得全名和電話號碼詳細信息但沒有縮略圖的推薦人的屏幕截圖 在此處輸入圖片說明

我做了很多嘗試,但無法找出問題所在。 任何幫助將不勝感激。

你有幾個問題。 主要問題從這里開始:

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

personDict[@userImage"]是一個UIImage 。由於某種原因,您可以通過圖像的description方法創建一個字符串,並將該無用的字符串存儲在contactImgPathArr

稍后,您嘗試通過傳入圖像的描述來濫用UIImage imageNamed:

因此,您需要進行兩項更改。 首先,更改:

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

至:

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

(更好的是:)

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

然后更改:

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

至:

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

(甚至更好:)

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

順便說一句-您對personDict的使用也毫無意義。

這段代碼:

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"]]];

應該只是:

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

當您可以簡單地使用someStringValue[NSString stringWithFormat:@"%@", someStringValue]請勿使用[NSString stringWithFormat:@"%@", someStringValue]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM