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

标签 ios objective-c xcode xmpp xmppframework

目前我正在开发基于聊天的应用程序。我已成功上传我的个人资料图片。任何人都可以建议我如何获取我的 friend 列表、个人资料图片和状态。我尝试了下面的代码。我正在使用 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()];

最佳答案

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

我使用这段代码来获取用户。获取用户后可以使用user.photo获取用户图片,使用user.displayName获取用户名,NSFetchedResultsController对象的section属性>, 获取状态。 如果部分值为 0,则状态:可用,如果状态为 1:离开,否则为离线

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

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

关于ios - 任何人都可以建议我如何获取我的 friend 列表个人资料图片和状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39569078/

相关文章:

ios - Objective C 到 Swift 的互操作性问题

ios - 与 UIButton 一起动态发送字符串?

ios - 如果以模态方式呈现另一个 View ,则保持 AVSpeechSynthesizer 播放

ios - iOS 12-NSFaceIDUsageDescription突然不起作用

ios - 如何在 Swift 中使用 Timer(以前称为 NSTimer)?

php - 上传图像选择器照片 ios

c++ - 如何从 CVPixelBufferRef 转换为 openCV cv::Mat

ios - 音频播放器在后台播放,应该基于硬件静音开关工作

ios - 了解task_basic_info任务resident_size

swift - 无法安装 AudioKit 4.1