2

我们如何通过传递手机号码作为参数从通讯录中获取人员的联系人图像。我不想打开通讯录。

我有一个图像井和另一个带有数字的文本字段和一个用于获取图像的按钮。

我只想在文本字段中输入 10 位数字。然后按下按钮,然后我想从通讯簿中获取具有此号码的人的缩略图。我只想从我的 UIView 传递的手机号码中获取人的缩略图。但我不想打开地址簿并从列表中选择任何联系人到我的 UIView 页面。

请任何人帮助我获取代码。我是 iphone 开发的新手。

4

1 回答 1

2

检查从电话号码获取图像的示例

NSString phoneNumber = @"yourPhoneNumber";
UIImage *myContactImage;
ABAddressBookRef addressBook = ABAddressBookCreate();

    // Get all contacts in the addressbook
    NSArray *allPeople = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);

    for (id person in allPeople) {
        // Get all phone numbers of a contact
        ABMultiValueRef phoneNumbers = ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonPhoneProperty);

        // If the contact has multiple phone numbers, iterate on each of them
        for (int i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
            NSString *phone = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, i);

            // Remove all formatting symbols that might be in both phone number being compared
            NSCharacterSet *toExclude = [NSCharacterSet characterSetWithCharactersInString:@"/.()- "];
            phone = [[phone componentsSeparatedByCharactersInSet:toExclude] componentsJoinedByString: @""];
            phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:toExclude] componentsJoinedByString: @""];

            if ([phone isEqualToString:phoneNumber]) {
                NSData *contactImageData = (NSData*)ABPersonCopyImageData(person);
                myContactImage = [[UIImage alloc] initWithData:contactImageData];
                break;
                break;
            }
        }
    }
于 2012-12-28T13:39:11.630 回答