我一直试图从地址簿中获取用户的联系人图片但还没有成功。我能够检索用户的名字,但似乎无法弄清楚图像的问题。
这是我到目前为止所尝试的。
-(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
}
答案 0 :(得分:0)
您可以使用以下代码行获取图像: -
if (ABPersonHasImageData(ref))
{
NSData *imgData = (__bridge NSData *) ABPersonCopyImageDataWithFormat(ref, kABPersonImageFormatThumbnail);
image = [UIImage imageWithData:imgData];
}
else
{
image = [UIImage imageNamed:@"default.png"];
}
检查为Martin R
表示您的self.reterivedImagesArray
已分配或不是
self.reterivedImagesArray=[[NSMutableArray alloc]init]