根据名称访问通讯录中的联系人图像 [英] Access contact image from address book based on name
问题描述
Err,好几天以来,我一直在思考一种方法。我检索了所有联系人的姓名,并使用字典将其放置在数组中。
Err,I have been pulling my hair thinking about a way from quite a few days.I have retrieved all contacts names and placed in an array using dictionary.
我所拥有的是一个模型类,其中包含一个名称列表,现在我想在联系人列表中搜索名称的位置,具体取决于我可以检索所需的联系人图像。
What I have is a model class holding a list of names,now I want to search the location of name in contacts list,depending on which I can retrieve the required contact image.
最初用Google搜索并发现了一个与我的要求不太相似的未解决问题,可以浏览
Initially googled and found out an unanswered question not pretty much similar to my requirement,the same can be glanced here
我尝试了几种方法,以下是我实现的一种方法:
I tried several ways,the below is one way I have implemented:
编辑
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[self loadReminders];
ReminderClass *reminderToDisplay = [self.remindersArray objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
// Now create the cell to display the reminder data
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
}
tableView.backgroundColor = [UIColor clearColor];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease];
[dateFormat setDateFormat:kDateFormat];
NSDate *reminderDate = [dateFormat dateFromString:reminderToDisplay.Date];
[dateFormat setDateFormat:kMinDateFormat];
NSString *dateString = [dateFormat stringFromDate:reminderDate];
NSString *valueString = [NSString stringWithFormat:@"%@'s %@",reminderToDisplay.Name,reminderToDisplay.Event];
NSString *onString = [NSString stringWithFormat:@" on %@",dateString];
NSString *reminderDetailsString = [valueString stringByAppendingString:onString];
//Get the contact image based on name index from contact list
ABAddressBookRef addressBook = ABAddressBookCreate( );
CFStringRef reminderName = (CFStringRef)reminderToDisplay.Name;
CFArrayRef allPeople = ABAddressBookCopyPeopleWithName(addressBook, reminderName);
self.contactsList =[[[NSMutableArray alloc]init]autorelease];
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for ( int i = 0; i < nPeople; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
NSString *contactFirstNamePart = (NSString *)ABRecordCopyValue(ref,kABPersonFirstNameProperty);
NSString *contactFirstName = [[[NSString alloc] initWithString:contactFirstNamePart]autorelease];
NSString *contactLastNamePart = (NSString *)ABRecordCopyValue(ref, kABPersonLastNameProperty);
if (contactLastNamePart == nil)
{
self.contactName = contactFirstName;
}
else
{
NSString *contactLastName = [[[NSString alloc] initWithString:contactLastNamePart]autorelease];
NSString *contactLastNameString = [NSString stringWithFormat:@" %@",contactLastName];
self.contactName = [contactFirstName stringByAppendingString:contactLastNameString];
CFRelease(contactLastNamePart);
}
NSDictionary *contactsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:self.contactName, kContactName, [NSNumber numberWithInt:i], kContactIndex, nil];
[self.contactsList addObject:contactsDictionary];
CFRelease(contactFirstNamePart);
}
NSDictionary *contactsDictionary = [self.contactsList objectAtIndex:indexPath.row];
self.contactName = [contactsDictionary objectForKey:kContactName];
int addressIndex = [[contactsDictionary objectForKey:kContactIndex]integerValue];
ABRecordRef recordReference = CFArrayGetValueAtIndex(allPeople, addressIndex);
if (ABPersonHasImageData(recordReference))
{
NSData *imageData = (NSData *)ABPersonCopyImageData(recordReference);
self.reminderImage = [UIImage imageWithData:imageData];
CFRelease(imageData);
}
CFRelease(allPeople);
CFRelease(addressBook);
UIImage *notificationImage = reminderImage;
if (notificationImage != nil)
{
UIImageView *imageView=[[[UIImageView alloc] initWithFrame:CGRectMake(240, 3, 70, 63)]autorelease];
imageView.backgroundColor=[UIColor clearColor];
[imageView setImage:notificationImage];
cell.accessoryView = imageView;
}
else
{
UIImageView *imageView=[[[UIImageView alloc] initWithFrame:CGRectMake(240, 3, 70, 63)]autorelease];
imageView.backgroundColor=[UIColor clearColor];
UIImage *defaultImage = [UIImage imageNamed:kDefaultImage];
[imageView setImage:defaultImage];
cell.accessoryView = imageView;
}
cell.textLabel.text = reminderDetailsString;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
错误访问错误屏幕截图
但是我无法完成所需的任务。任何人都可以指导我。
But I was unable to accomplish the required task.Can any one please guide me.
在此先谢谢大家:)
推荐答案
我正在共享我最近的一个应用程序中使用的示例代码片段。我已进行修改以满足您的要求,还请注意,我已经在记事本中对此进行了编辑,并且可能会出现一些拼写错误。(目前,我没有用Mac对其进行测试。.:P)
基本思想是填补viewDidLoad方法中的数据源,并使用该数据源更新tableView。希望这将是解决您的问题的投入。
I am sharing the sample code snippet I used in one of my recent app. I have modified to fit it ur requirements and also please note that I have edited this in notepad and may have some typo errors.(Currently I dnt have mac to test it..:P) Basic idea is to fill the datasource in viewDidLoad method and use that dataSource to update the tableView. Hope this will be an input to solve your problem.
viewDidLoad
contactsToBeAdded=[[NSMutableArray alloc] init];
ABAddressBookRef addressbook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressbook);
CFIndex numPeople = ABAddressBookGetPersonCount(addressbook);
bool hasPhoneNumber = false;
for (int i=0; i < numPeople; i++) {
hasPhoneNumber = false;
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
ABMutableMultiValueRef phonelist = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex numPhones = ABMultiValueGetCount(phonelist);
if(numPhones > 0){
hasPhoneNumber = true;
}
if(hasPhoneNumber){
NSString *firstName=(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName=(NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
CFTypeRef ABphone = ABMultiValueCopyValueAtIndex(phonelist, 0);
NSString *personPhone = (NSString *)ABphone;
NSMutableDictionary *dictToAdd = [[[NSMutableDictionary alloc]init]autorelease];
if(firstName != nil && firstName != NULL){
[dictToAdd setObject:firstName forKey:@"firstName"];
CFRelease(firstName);
}
else{
[dictToAdd setObject:@"" forKey:@"firstName"];
}
if(lastName != nil && lastName != NULL){
[dictToAdd setObject:lastName forKey:@"lastName"];
CFRelease(lastName);
}
else{
[dictToAdd setObject:@"" forKey:@"lastName"];
}
if(personPhone != nil && personPhone != NULL){
[dictToAdd setObject:personPhone forKey:@"mobile"];
CFRelease(ABphone);
}
else{
[dictToAdd setObject:@"" forKey:@"mobile"];
}
//Get the first name and last name added to dict and combine it to full name
NSString *firstName = [dictToAdd objectForKey:@"firstName"];
NSString *lastName = [dictToAdd objectForKey:@"lastName"];
NSString *fullName = [firstName stringByAppendingString:lastName];
//Now check whether the full name is same as your reminderToDisplay.Name
if(reminderToDisplay.Name isEqualToString:fullName )
{
CFDataRef imageData = ABPersonCopyImageData(person);
UIImage *image = [UIImage imageWithData:(NSData *)imageData];
if(image != nil && image != NULL){
[dictToAdd setObject:image forKey:@"image"];
CFRelease(imageData);
}
else{
[dictToAdd setObject:[UIImage imageNamed:TEMP_IMG] forKey:@"image"];
}
}
[contactsToBeAdded addObject:dictToAdd];
}
CFRelease(phonelist);
}
CFRelease(allPeople);
CFRelease(addressbook);
[self.tableView reloadData];
numberOfRowsInSection
return contactsToBeAdded.count;
cellForRowAtIndexPath
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
NSDictionary *contactToAdd;
//This way you can get the data added in viewDidLoad method
contactToAdd = [contactsToBeAdded objectAtIndex:indexPath.row];
NSString *fName = (NSString *)[contactToAdd objectForKey:@"firstName"];
NSString *lName = (NSString *)[contactToAdd objectForKey:@"lastName"];
UIImage *contactImg = (UIImage*)[contactToAdd objectForKey:@"image"];
这篇关于根据名称访问通讯录中的联系人图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!