Search By Number and Get the image using ABAddressBook

前端 未结 2 896
自闭症患者
自闭症患者 2020-12-25 09:04

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 UIImageVie

相关标签:
2条回答
  • 2020-12-25 09:13
    -(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;
                }
    
    
            }
    
        }
    }
    
    0 讨论(0)
  • 2020-12-25 09:23

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

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

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

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

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

    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.

    0 讨论(0)
提交回复
热议问题