如何通过传递手机号码作为参数,从地址簿中获取人物的联系图像

时间:2012-12-28 13:28:27

标签: iphone objective-c ios

如何通过传递手机号码作为参数,从地址簿中获取人物的联系人形象。我不想打开地址簿。

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

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

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

1 个答案:

答案 0 :(得分: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;
            }
        }
    }