简体   繁体   English

如何在iOS 6中从通讯簿中获取联系人图像

[英]How to fetch contact images from Address Book in iOS 6

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

}

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 检查Martin R说的是你的self.reterivedImagesArray是否已分配

self.reterivedImagesArray=[[NSMutableArray alloc]init]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM