[英]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. 我正在使用UITableView显示移动联系人详细信息,例如全名,电话号码和个人资料图像。 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日志中生成的内容
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
. personDict[@userImage"]
是一个UIImage
。由于某种原因,您可以通过图像的description
方法创建一个字符串,并将该无用的字符串存储在contactImgPathArr
。
Later, you attempt to misuse UIImage imageNamed:
by passing in the image's description. 稍后,您尝试通过传入图像的描述来滥用
UIImage imageNamed:
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. 顺便说一句-您对
personDict
的使用也毫无意义。
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
. 当您可以简单地使用
someStringValue
时[NSString stringWithFormat:@"%@", someStringValue]
请勿使用[NSString stringWithFormat:@"%@", someStringValue]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.