我一直在尝试从通讯簿中获取用户的联系人图像,但尚未成功。我能够检索用户的姓名,但似乎无法找出图像的问题。
这是我到目前为止所尝试的。
-(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
}