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
}