无法从联系簿中获取任何联系人的图片

时间:2016-02-17 08:07:47

标签: ios objective-c addressbook

以下是我的代码,但是使用此代码我无法获得任何联系人的图片。 请帮助我。

- (void)loadAddressBook
{
arrContacts = [[NSMutableArray alloc]init];
APAddressBook *addressBook = [[APAddressBook alloc] init];
[addressBook loadContacts:^(NSArray <APContact *> *contacts, NSError *error)
 {
     if (!error)
     {
         for (APContact *strcontact in contacts)
         {
             NSMutableDictionary *addressDict=[[NSMutableDictionary alloc]init];
             [addressDict setValue:strcontact.name.compositeName forKey:COMPOSITENAME];
             if (strcontact.thumbnail != nil)
             {
                 NSData *imgData = [NSData dataWithData:UIImagePNGRepresentation(strcontact.thumbnail)];
                 // From data to string
                 NSString *strImage = [[NSString alloc] initWithData:imgData encoding:NSUTF8StringEncoding];
                 [addressDict setObject:strImage forKey:IMAGE];
             }else {
                 [addressDict setObject:NOIMAGE forKey:IMAGE];
             }
             for (APPhone *phones in strcontact.phones) {
                 //  NSLog(@"Number %@",phones.number);
                 [addressDict setValue:phones.number forKey:PHONE];
             }
             for (APEmail *emails in strcontact.emails) {
                 //   NSLog(@"Emails %@",emails.address);
                 [addressDict setValue:emails.address forKey:EMAIL];
             }
             for (APAddress *address in strcontact.addresses) {
                 //  NSLog(@"Number %@",phones.number);
                 [addressDict setValue:address forKey:ADDRESS];
             }
             NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
             formatter.dateFormat = @"dd.MM.yyyy";
             NSString *strDate = [formatter stringFromDate:[NSDate date]];
             [addressDict setValue:strDate forKey:IMPORTDATE];
             [arrContacts addObject:addressDict];
         }
         [tblmportContacts reloadData];
     }
     else
     {
         // show error
     }
 }];
}

我正在获取所有细节,但无法从APAddressBook获取图像。 请为此提供解决方案。

1 个答案:

答案 0 :(得分:1)

尝试以下代码。

addressBook.fieldsMask = APContactFieldAll;