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

标签 iphone objective-c ios

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

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

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

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

最佳答案

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

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;
            }
        }
    }

关于iphone - 如何通过传递手机号码作为参数从地址簿中获取人员的联系人图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14070265/

相关文章:

android - 开源移动应用程序

iphone - 应用程序中的多个用户配置文件/帐户 - 持久性

ios - 如何在没有关联的 UI 元素的情况下在 iOS 中创建一个 segue

ios - 具有右图像和居中文本的 UIButton

ios - 我如何在 UIProgressView 中设置进度图像

ios - 标签/按钮上的约束以显示标签的整个文本

ios - Swift 1.2 - 在 Xcode 6.3.2 和 Xcode 4 中找不到桥接头文件消息

ios - 如何使用Apple Transport Security保护HTTPS .m4v URL

ios - 是否可以隐藏或替换已编译的第三方库的 NSLog() 函数,该库不断向我的日志中喷射调试?

ios - 使用 iOS 每 x 分钟发送一次 GPS 坐标 - Core Location 不会停止更新