【问题标题】:Search By Number and Get the image using ABAddressBook按编号搜索并使用 ABAddressBook 获取图像
【发布时间】:2011-02-25 10:12:34
【问题描述】:

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

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

谁能给我建议我可以遵循的解决方案或任何替代路径。任何代码 sn-p 也会有很大帮助!!

任何形式的帮助都是非常值得赞赏的。

提前致谢

【问题讨论】:

    标签: xcode uiimageview abaddressbook


    【解决方案1】:

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

    ABAddressBookRef addressbook = ABAddressBookCreate();
    

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

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

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

    ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
    

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

    ABMutableMultiValueRef phonelist = ABRecordCopyValue(person, kABPersonPhoneProperty);
    

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

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

    由于电话号码有与之关联的数字和标签,您需要将实际的电话号码字符串提取为 NSString:

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

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

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

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

    当需要退出时,您需要清理内存。 AB 框架的一般经验法则是,函数名称中带有Get 的任何内容都不需要发布,而带有CopyCreate 的任何内容都需要发布。因此,在这种情况下,您需要 CFRelease() phonelistallPeopleaddressbook,但不需要 numPeoplepersonnumPhones

    【讨论】:

    • Jeff 先生,看来您对 ABAddressBook 框架和关键特性有很好的掌握。请您花一两分钟了解一下我的问题:stackoverflow.com/questions/15138946/…。期待尽快收到回复,谢谢:)
    【解决方案2】:
    -(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;
                }
    
    
            }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多