Ios 在目标c中使用联系人框架时,无法在tableview中显示联系人缩略图图像
我正在使用UITableView显示手机联系人的详细信息,如全名、电话号码和个人资料图像。下面是代码Ios 在目标c中使用联系人框架时,无法在tableview中显示联系人缩略图图像,ios,objective-c,iphone,uitableview,Ios,Objective C,Iphone,Uitableview,我正在使用UITableView显示手机联系人的详细信息,如全名、电话号码和个人资料图像。下面是代码 - (void)viewDidLoad { [super viewDidLoad]; [self fetchContactsandAuthorization]; [_selectContactListTblView reloadData]; } -(void)fetchContactsandAuthorization { // Request autho
- (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;
}
contactImg-(
", {196, 199}",
", {196, 199}",
", {196, 199}",
", {196, 199}",
", {196, 199}",
", {3000, 2002}",
", {196, 199}",
", {196, 199}",
", {196, 199}",
", {196, 199}",
", {196, 199}",
", {196, 199}"
)
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*simpleTableIdentifier=@“SelectContactTableViewCell”;
选择ContactTableViewCell*cell=(选择ContactTableViewCell*)[tableView出列可重用CellWithIdentifier:simpleTableIdentifier];
如果(单元格==nil)
{
NSArray*nib=[[NSBundle mainBundle]loadNibNamed:@“SelectContactTableViewCell”所有者:自身选项:nil];
单元格=[nib objectAtIndex:0];
}
cell.contactFullName.text=[contactFullNameArr objectAtIndex:indexath.row];
//尝试直接赋值,但没有用
cell.contactProfileImage.image=[contactImgPathArr objectAtIndex:indexath.row];
//如果我指定任何静态图像,我就能够看到该图像
//cell.contactProfileImage.image=[UIImage ImageName:@“person icon.png”];
cell.contactPhoneNumber.text=[contactPhoneNumberObjectatindex:indexath.row];
返回单元;
}
我获得全名和电话号码详细信息但没有缩略图的refernece的屏幕截图
我试了很多,但没能找出问题所在。任何帮助都将不胜感激 您有一些问题。主要问题从这里开始:
[contactImgPathArr addObject:[NSString stringWithFormat:@"%@",[personDict objectForKey:@"userImage"]]];
personDict[@userImage”]
是一个UIImage
。出于某种原因,您可以从图像的描述
方法创建一个字符串,并将该无用字符串存储在联系人imgpatharr
中
稍后,您试图通过传入图像的描述来误用
UIImage ImageName:
所以你需要两个改变。第一,改变:
[contactImgPathArr addObject:[NSString stringWithFormat:@"%@",[personDict objectForKey:@"userImage"]]];
致:
(更好的是:)
然后改变:
cell.contactProfileImage.image = [UIImage imageNamed:[contactImgPathArr objectAtIndex: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
UIImage imageNamed:
时,切勿使用[NSString stringWithFormat:@“%@”,someStringValue]
并且仅适用于应用程序资源包中的图像。您是对的,但将此uiimage文件分配给imageview是我无法解决的问题。@rmaddyGreat解释并感谢您提供的宝贵建议。我会记住这一点。这很有效。
cell.contactProfileImage.image = [contactImgPathArr objectAtIndex:indexPath.row];
cell.contactProfileImage.image = contactImgPathArr[indexPath.row];
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];