任何人都可以建议我如何获取我的朋友列表个人资料图片和状态

时间:2016-09-19 08:54:39

标签: ios objective-c xcode xmpp xmppframework

目前我正在使用基于聊天的应用程序。我已成功上传我的个人资料照片。任何人都可以建议我如何获取我的朋友列表,个人资料图片和状态。我试过下面的代码。我正在使用XMPP框架进行聊天。

此代码用于恢复我自己的Prifile图片

    xmppvCardStorage = [XMPPvCardCoreDataStorage sharedInstance];
xmppvCardTempModule= [[XMPPvCardTempModule alloc] initWithvCardStorage:xmppvCardStorage];

XMPPvCardAvatarModule *xmppvCardAvatarModule = [[XMPPvCardAvatarModule alloc] initWithvCardTempModule:xmppvCardTempModule];

XMPPJID *jid = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@localhost",str]];
NSData *avtarData = [xmppvCardAvatarModule photoDataForJID:jid];

  **This code for Upload my own Prifile picture**

 NSXMLElement *vCardXML = [NSXMLElement elementWithName:@"vCard" xmlns:@"vcard-temp"];
XMPPvCardTemp *newvCardTemp = [XMPPvCardTemp vCardTempFromElement:vCardXML];

[newvCardTemp setGivenName:@"gireesh"];
[newvCardTemp setSortString:@"Chanti"];


NSArray *arr1= [[NSUserDefaults standardUserDefaults]objectForKey:@"USER"];
[[NSUserDefaults standardUserDefaults]synchronize];
DLog(@"user Profile %@",[arr1 objectAtIndex:0]);
DLog(@"user Profile %@",[arr1 objectAtIndex:1]);
NSString *str = [NSString stringWithFormat:@"%@%@",[arr1 objectAtIndex:1],[arr1 objectAtIndex:0]];




[newvCardTemp setJid:[XMPPJID jidWithString:[NSString stringWithFormat:@"%@@localhost",str]]];
[newvCardTemp setFormattedName:@""];
[newvCardTemp setEmailAddresses:[getdic valueForKey:@"emailid"]];


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imageFilePath = [documentsDirectory stringByAppendingPathComponent:@"Profileimage"];

UIImage* image1 = [UIImage imageWithContentsOfFile:imageFilePath];
NSData *imageData1 = UIImagePNGRepresentation(image1);

[newvCardTemp setPhoto:imageData1];
[xmppvCardTempModule updateMyvCardTemp:newvCardTemp];
[xmppvCardTempModule activate:[self appDelegate].xmppStream];
[xmppvCardTempModule addDelegate:self delegateQueue:dispatch_get_main_queue()];

1 个答案:

答案 0 :(得分:1)

从XMPPUserCoreData获取用户对象,然后使用用户对象获取用户列表的照片和名称。

我使用此代码来获取用户。获取用户后您可以使用user.photo获取用户图像,user.displayName获取用户名,使用NSFetchedResultsController对象的section属性来获取状态。 如果部分值为0,则状态为:Available,如果状态为1:Away,则为Offline

- (NSFetchedResultsController *)fetchedResultsControllerContacts
{
    if (fetchedUserLists == nil)
    {
        NSManagedObjectContext *moc = [[AppDelegate delegate] managedObjectContext_roster];

        NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorageObject"
                                                  inManagedObjectContext:moc];

        NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"sectionNum" ascending:YES];
        NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES];

        NSArray *sortDescriptors = [NSArray arrayWithObjects:sd1, sd2, nil];

        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        [fetchRequest setEntity:entity];
        [fetchRequest setSortDescriptors:sortDescriptors];
        [fetchRequest setFetchBatchSize:10];

        fetchedUserLists = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                           managedObjectContext:moc
                                                                             sectionNameKeyPath:@"sectionNum"
                                                                                      cacheName:nil];
        [fetchedUserLists setDelegate:(id)self];
        NSError *error = nil;
        if (![fetchedUserLists performFetch:&error])
        {
            DDLogError(@"Error performing fetch: %@", error);
        }
    }
    return fetchedUserLists;
}

获取部分数量 - >计数

NSArray *sections = [[self fetchedResultsControllerContacts] sections];

获取numberOfRowsInSection方法

中一个部分的行数
if (section < [sections count])
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:sectionIndex];
    return sectionInfo.numberOfObjects;
}

获取单个用户 在cellForRowAtIndexPath

XMPPUserCoreDataStorageObject *user = [[self fetchedResultsControllerContacts] objectAtIndexPath:indexPath];

希望它有所帮助。 快乐的编码...