按编号搜索并使用 ABAddressBook 获取图像 [英] Search By Number and Get the image using ABAddressBook

查看:20
本文介绍了按编号搜索并使用 ABAddressBook 获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

I tried using ABAddressBook framework but was clueless to proceed.

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

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.

提前致谢

推荐答案

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

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:

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

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

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:

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++) {

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

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:

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

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

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

一旦找到电话号码与 inNumber 匹配的人,您需要将该人的图像提取到 UIImage 中:

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

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

到了退出的时候,你需要清理内存.AB 框架的一般经验法则是,函数名称中带有 Get 的任何内容都不需要发布,以及任何带有 CopyCreate,你确实需要释放.因此,在这种情况下,您需要 CFRelease() phonelistallPeopleaddressbook,但不需要numPeoplepersonnumPhones.

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 Get in the function name you don't need to release, and anything with Copy or 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.

这篇关于按编号搜索并使用 ABAddressBook 获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆