get Image from Contact stored in AddressBook [iPhone]

Using this code you can retrieve the pictures of contact stored in your AddressBook!

ABAddressBookRef addressBook = ABAddressBookCreate();



NSMutableArray *people = [[[(NSArray*) ABAddressBookCopyArrayOfAllPeople(addressBook) autorelease] mutableCopy] autorelease];

[people sortUsingFunction:(int (*)(id, id, void *) ) ABPersonComparePeopleByName context:(void*)ABPersonGetSortOrdering()];

UIImage* image;
for (int i = 0; i < [people count]; i++)
{
    ABRecordRef person = [people objectAtIndex:i];

    if(ABPersonHasImageData(person)){
        image = [UIImage imageWithData:(NSData *)ABPersonCopyImageData(person)];
    }else{
        image = [UIImage imageNamed:@"contact_image.gif"];
    }
}

Popularity: 6% [?]

My name is Adeem M Basraa. I am a software engineer from Lahore, PK. I’ve been involved in software development for nearly 4 years, with the last 1 year focused on application development for mobile devices (iPhone & Android).

6 Comments on "get Image from Contact stored in AddressBook [iPhone]"

  1. Asif says:

    How can we display address book images in our application

  2. admin says:

    yep you can do that. the code i already written get all the images from contact list , you just need to stored those images in some mutable array and used it anywhere you likes :)

  3. Amit says:

    I have more than 2k contacts to read form addressbook, but the reading code crashes ( low memory) after reading 2k records. Application have no leak in contacts reading code, i checked with leak application.

  4. Amit,

    the leak is in the line:
    image = [UIImage imageWithData:(NSData *)ABPersonCopyImageData(person)];

    which instead should read something like:

    CFDataRef imageData = ABPersonCopyImageData(person)];
    image = [UIImage imageWithData:(NSData *)imageData];
    CFRelease(imageData);

  5. Sasha says:

    Hey,
    Where/how exactly do we have to execute this code ?

Trackbacks for this post

  1. get Image from Contact stored in AddressBook « iPhone Diary

Got something to say? Go for it!