MOBILE

Iphone SDK : Working with the Address Book Database - Person Photo Retriever Application

11/6/2012 1:12:42 AM
In this section, we present a sample application that prompts the user to enter a query for a person. This query is used to retrieve a person with a photo and display it on the screen as shown in Figure 1

The source code for this application can be found in the PersonPhotoRetriever project available from the source downloads.

Listing 1 shows a category on UIImage to return the photo of the first person in the address book whose name matches a given query.

Figure 1. The photo retriever application.

Example 1. A category on UIImage to return the photo of the first person in the address book whose name matches a given query.
@interface  UIImage (AddressBook)
+(UIImage*)photoForAperson:(NSString*)_name;
@end 

@implementation  UIImage (AddressBook)
// returns the image of the first person give a query 
+(UIImage*)photoForAperson:(NSString*)_name{
  UIImage *_image = nil;
  ABAddressBookRef addressBook = ABAddressBookCreate();
  NSArray    *people = (NSArray*)
    ABAddressBookCopyPeopleWithName(addressBook, (CFStringRef)_name);
  int  index = 0;
  while  (index < [people count]){
    ABRecordRef person = (ABRecordRef)[people objectAtIndex:index++];
    NSData *photoData = (NSData*) ABPersonCopyImageData(person);
    if (photoData){
      _image = [UIImage imageWithData:photoData];

[photoData release];
      break;
    }
  }
  [people release];
  CFRelease(addressBook);
  return _image;
}
@end

The photoForAperson: method takes the query text as an argument and returns the photo. It starts by creating an address book object. After that, the method ABAddressBookCopyPeopleWithName is used to perform a prefix search using the given query. The function returns an array of person records that match this query. The method then iterates through this array retrieving the image data of each person record using ABPersonCopyImageData function. If the photo data is not nil, a UIImage instance is created from this data and the image is returned.

Listing 2 shows the method triggered when the user taps the Retrieve Photo button. It simply retrieves the image view and sets it to the image if an image with the query exists. Otherwise, it displays an alert view.

Example 2. The method triggered when the user taps the Retrieve Photo button.
-(void)buttonPushed{
  NSString  *name = [(UITextField*)[self.view viewWithTag:999] text];
  UIImageView *imageView = (UIImageView*)[self.view viewWithTag:1234];
  UIImage *image = [UIImage photoForAperson:name];
  if(image){
    imageView.image = image;
    [[self.view viewWithTag:999] resignFirstResponder];
   }
  else{
    [[[UIAlertView alloc] initWithTitle:@"No Records!"
      message:[NSString stringWithFormat:@"%@ photo not found", name]
      delegate:self
      cancelButtonTitle:nil
      otherButtonTitles:@"OK", nil] show];
  }
}
Other  
 
Top 10
3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
3 Tips for Maintaining Your Cell Phone Battery (part 1) - Charge Smart
OPEL MERIVA : Making a grand entrance
FORD MONDEO 2.0 ECOBOOST : Modern Mondeo
BMW 650i COUPE : Sexy retooling of BMW's 6-series
BMW 120d; M135i - Finely tuned
PHP Tutorials : Storing Images in MySQL with PHP (part 2) - Creating the HTML, Inserting the Image into MySQL
PHP Tutorials : Storing Images in MySQL with PHP (part 1) - Why store binary files in MySQL using PHP?
Java Tutorials : Nested For Loop (part 2) - Program to create a Two-Dimensional Array
Java Tutorials : Nested For Loop (part 1)
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
VIDEO TUTORIAL
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
Popular Tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS