xcode 按编号搜索并使用 ABAddressBook 获取图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5115977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:19:20  来源:igfitidea点击:

Search By Number and Get the image using ABAddressBook

xcodeuiimageviewabaddressbook

提问by devsri

I wish to search in the iphone AddressBook through my app using the number as the key and then retrieve the image associated to that contact and display it on the UIImageView.

我希望通过我的应用程序使用数字作为键在 iphone 地址簿中搜索,然后检索与该联系人关联的图像并将其显示在 UIImageView 上。

I tried using ABAddressBook framework but was clueless to proceed.

我尝试使用 ABAddressBook 框架,但无能为力。

Can anyone please suggest me the solutions or any alternative path that I can follow. Any code snippet would also be of great help!!

任何人都可以向我建议我可以遵循的解决方案或任何替代路径。任何代码片段也会有很大帮助!!

Any form of help would be highly appreciable.

任何形式的帮助都是非常可观的。

Thanks in advance

提前致谢

回答by Jeff Hay

The AB framework can be a real pain at times. But it breaks down to a series of pretty simple operations. First, you have to create an ABAddressBook instance:

AB 框架有时可能是一个真正的痛苦。但它分解为一系列非常简单的操作。首先,您必须创建一个 ABAddressBook 实例:

ABAddressBookRef addressbook = ABAddressBookCreate();

Then you'll want to make a copy of the array of all people in the address book, and step through them looking for the data you want:

然后,您需要复制地址簿中所有人的数组,并逐步通过他们查找您想要的数据:

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressbook);
CFIndex numPeople = ABAddressBookGetPersonCount(addressbook);
for (int i=0; i < numPeople; i++) { 

Inside your loop, you'll probably want to get a reference to the individual person:

在您的循环中,您可能希望获得对个人的引用:

ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

Then you want to compare the number you have (lets call that inNumber) to every phone number associated with that particular person. To do that, you first need a list of all the person's phone numbers:

然后,您想将您拥有的号码(让我们称之为inNumber)与与该特定人员关联的每个电话号码进行比较。为此,您首先需要一个包含所有人员电话号码的列表:

ABMutableMultiValueRef phonelist = ABRecordCopyValue(person, kABPersonPhoneProperty);

Then, of course, you'll need to have an inner loop that loops over each of the individual person's phone numbers:

然后,当然,您需要有一个内部循环来循环每个人的电话号码:

CFIndex numPhones = ABMultiValueGetCount(phones);
for (int j=0; j < numPhones; j++) {

Since the phone numbers have both numbers and labels associated with them, you'll need to extract the actual phone number string as an NSString:

由于电话号码既有号码又有标签,因此您需要将实际电话号码字符串提取为 NSString:

CFTypeRef ABphone = ABMultiValueCopyValueAtIndex(phoneList, j);
NSString *personPhone = (NSString *)ABphone;
CFRelease(ABphone);

Now you can finally compare numbers! Do so with the standard NSString comparison methods, but remember that you need to worry about formatting, etc.

现在你终于可以比较数字了!使用标准的 NSString 比较方法这样做,但请记住,您需要担心格式等问题。

Once you find the person who has a phone number matching inNumber, you'll want the extract that person's image into a UIImage:

找到电话号码匹配的人后inNumber,您需要将该人的图像提取为UIImage

    CFDataRef imageData = ABPersonCopyImageData(person);
    UIImage *image = [UIImage imageWithData:(NSData *)imageData];
    CFRelease(imageData);

When it comes time to exit, you'll need to clean up memory. A general rule of thumb for the AB framework is that anything with Getin the function name you don't need to release, and anything with Copyor Create, you do need to release. So, in this case you'll need to CFRelease()phonelist, allPeople, and addressbook, but not numPeople, person, or numPhones.

当需要退出时,您需要清理内存。AB 框架的一般经验法则是,Get函数名称中包含的任何内容都不需要释放,而任何包含Copy或 的Create内容都需要释放。因此,在这种情况下,您需要CFRelease()phonelistallPeople、 和addressbook,但不需要numPeopleperson、 或numPhones

回答by Nidhin

-(void)fetchAddressBook:(NSString *)searchnumber
{
    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);

    //iterate through each record and add the value in the array
    for (int i =0; i<numberofPeople; i++) {

        ABRecordRef ref = CFArrayGetValueAtIndex(ContactInfoArray, i);

        NSString *firstName = (__bridge NSString *)ABRecordCopyValue(ref, kABPersonFirstNameProperty);
        //Get phone no. from contacts
        ABMultiValueRef multi = ABRecordCopyValue(ref, kABPersonPhoneProperty);
        UIImage *iimage;
        NSString* phone;
        for (CFIndex j=0; j < ABMultiValueGetCount(multi); j++) {
            iimage=nil;
            phone=nil;
            phone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(multi, j);



            //if number matches
            if([phone isEqualToString:searchnumber])
            {
                NSLog(@"equlas%@",searchnumber);

            //if person has image store it
            if (ABPersonHasImageData(ref)) {

                CFDataRef imageData=ABPersonCopyImageDataWithFormat(ref, kABPersonImageFormatThumbnail);
                iimage = [UIImage imageWithData:(__bridge NSData *)imageData];

            }else{
                //default image
                iimage=[UIImage imageNamed:@"icon"];

            }

         //set image and name
                userimage.image=iimage;
                lblname.text=firstName;

                return;
            }


        }

    }
}