0

我一直在尝试从通讯簿中获取用户的联系人图像,但尚未成功。我能够检索用户的姓名,但似乎无法找出图像的问题。

这是我到目前为止所尝试的。

   -(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

}
4

1 回答 1

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]
于 2013-09-02T11:24:53.523 回答