-1

I want to show phone contacts along with the pic in table view in alphabetic order.

I have managed to show contact name in correct order but unable to show images in correct order.

I have been trying these for few days but couldn't succeed .

I know how to sort array but don't know how to sort the image data.

please help me guys.

here is my demo code:

  NSArray *sortedFirstNames;

 - (void)getPersonDetails
 {
CFErrorRef error = NULL;

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

if (addressBook != nil)
{
    NSLog(@"Succesful.");

    NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

    NSUInteger i = 0;
    NSMutableArray *firstNames = [[NSMutableArray alloc] init];
    NSMutableArray *firstimages = [[NSMutableArray alloc] init];
    for (i = 0; i < [allContacts count]; i++)
    {
        Person *person = [[Person alloc] init];

        ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];




        NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
        NSString *lastName =  (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
        NSString *fullName;
        if (lastName == nil)
        {
            fullName = [NSString stringWithFormat:@"%@", firstName];
            [firstNames addObject: fullName];
        }
        else if(firstName ==nil)
        {

                fullName = [NSString stringWithFormat:@"%@", lastName];
                [firstNames addObject: fullName];

        }
        else
        {
            fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
            [firstNames addObject: fullName];

        }

        person.firstName = firstName;
        person.lastName = lastName;
        person.fullName = fullName;


        //email


       imgdata=(__bridge NSData*)ABPersonCopyImageDataWithFormat(contactPerson, kABPersonImageFormatThumbnail);

        NSLog(@"newstris%@",imageStr);
        if(imgdata==nil)
        {

        }
        else
        {
        [firstimages addObject:imgdata];
        }





        [self.tableData addObject:person];
    }
    sortedFirstNames = [firstNames sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];


}

CFRelease(addressBook);

}

code for showing images in cell:

  dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^{


        UIImage *image1;

        //        cell.imageView.frame = CGRectMake(0, 0, 50, 50);
        if (person.imgdata==nil) {
            image1=[UIImage imageNamed:@"default_profile_pic.jpg"];
        }
        else
        {
          image1 = [UIImage imageWithData:person.imgdata];  
        }
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image1];
        imageView.frame = CGRectMake(6.5, 6.5, 45., 45.);

        dispatch_sync(dispatch_get_main_queue(), ^{
            [cell addSubview:imageView];

        });
    });

Thanks..

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
Varun Vishnoi
  • 328
  • 6
  • 23

2 Answers2

1

Answer is pretty simple.

Add a field in your Person class which stores a NSData. Like:

 @property (nonatomic, strong) NSData *imageData;

And use:

person.imageData = imgdata;
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
0

I too faced same issue. I have fixed this issue by associate object like this.

while loading source array:

 objc_setAssociatedObject(hDetails, @"imageUrl", record, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

in cell for row at indexpath :

Record *record = objc_getAssociatedObject(hDetails,@"imageUrl");

for more reference see this :

How do I use objc_setAssociatedObject/objc_getAssociatedObject inside an object?

Community
  • 1
  • 1
Ganapathy
  • 4,594
  • 5
  • 23
  • 41