0

I have been trying to fetch contact image of user from address book with no success yet.I am able to retrieve names of user but can't seem to figure out the problems with images.

This is what i tried so far.

   -(void)fetchAddressBook
   {
    self.reterivedNamesMutableArray =[[NSMutableArray alloc]init];
    ABAddressBookRef UsersAddressBook = ABAddressBookCreateWithOptions(NULL, NULL);

   //contains details for all the contacts
   CFArrayRef ContactInfoArray = ABAddressBookCopyArrayOfAllPeople(UsersAddressBook);

  //get the total number of count of the users contact
  CFIndex numberofPeople = CFArrayGetCount(ContactInfoArray);

   UIImage* image;
  //iterate through each record and add the value in the array
  for (int i =0; i<numberofPeople; i++)
  {
    ABRecordRef ref = CFArrayGetValueAtIndex(ContactInfoArray, i);
    ABMultiValueRef firstName = (__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonFirstNameProperty));
    ABMultiValueRef lastName = (__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonLastNameProperty));


     NSString *tempFirstName = (__bridge NSString *)(firstName);
    NSString *tempLastName = (__bridge NSString *)(lastName);

    //Compose full name
    NSString *fullName = @"";

    if (firstName != nil)
    {
               fullName = [fullName stringByAppendingString:tempFirstName];
    }

    if (lastName != nil)
    {
                  fullName = [fullName stringByAppendingString:@" "];
                  fullName = [fullName stringByAppendingString:tempLastName];
    }

    if (ABPersonHasImageData(ref))
    {
        image = [UIImage imageWithData:(__bridge NSData *)(ABPersonCopyImageDataWithFormat(ref, kABPersonImageFormatThumbnail))];
    }
    else
    {
        image = [UIImage imageNamed:@"default.png"];
    }
    [self.reterivedNamesMutableArray addObject:fullName];     //Array of full name.
    [self.reterivedImagesArray addObject:image];              //Array of contact images.
     NSLog(@"Names array content = %@", self.reterivedNamesMutableArray );
     NSLog(@"Images array content = %@", [self.reterivedImagesArray lastObject]);//This shows null

}
iCodes
  • 1,382
  • 3
  • 21
  • 47
  • Have you allocated+initialized self.reterivedImagesArray ? – Martin R Sep 02 '13 at 11:26
  • Yes i did that already......I deleted the project from simulator and restarted it....and its seems to work now.... – iCodes Sep 02 '13 at 11:37
  • Please note that your code has lots of memory leaks. You have to `CFRelease()` the objects returned from "Create" or "Copy" functions, or use `__brigde_transfer` or `CFBridgingRelease()` at appropriate places. I would recommend to run "Product -> Analyze" in Xcode, and read the memory management guidelines. – Martin R Sep 02 '13 at 11:40

1 Answers1

0

you can get image using this line of code:-

if (ABPersonHasImageData(ref))
    {
        NSData  *imgData = (__bridge NSData *) ABPersonCopyImageDataWithFormat(ref, kABPersonImageFormatThumbnail);
        image = [UIImage imageWithData:imgData];
    }
    else
    {
        image = [UIImage imageNamed:@"default.png"];
    }

Check as Martin R says's your self.reterivedImagesArray is Allocated or not like

self.reterivedImagesArray=[[NSMutableArray alloc]init]
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
  • I have done the same thing....But when i NSLog the array where i store the image , it shows null. – iCodes Sep 02 '13 at 11:28
  • as martin say's are you allocated the array *self.reterivedImagesArray* like self.reterivedImagesArray=[[NSMutableArray alloc]init] – Nitin Gohel Sep 02 '13 at 11:29