2013-06-28 77 views
-1

我想按字母顺序显示手机联系人以及桌面视图中的图片。在桌面视图中显示联系人图像以及联系人姓名

我设法以正确的顺序显示联系人姓名,但无法以正确的顺序显示图像。

我一直在尝试这些几天,但不能成功。

我知道如何排序数组,但不知道如何排序图像数据。

请帮帮我吧。

这里是我的演示代码:用于显示在细胞图像

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

} 

代码:

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

     }); 
    }); 

谢谢..

回答

1

答案是非常简单的。

在您的Person类中添加一个字段,其中会存储NSData。 像:

@property (nonatomic, strong) NSData *imageData; 

及用途:

person.imageData = imgdata; 
+0

感谢您的回答,我已经做到了,但图像没有显示相关的名称。我想要图像以及那里的名称在阿尔法(A-Z) –

+0

您确定图像数据在cellForRowAtIndexPath中不为空吗? –

+0

@VarunVishnoi:你使用过财产吗? –

相关问题