如何从用户的Blob ID向对话框提供照片? [英] How to give photo to dialog from user's blob id?

查看:59
本文介绍了如何从用户的Blob ID向对话框提供照片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Quickblox,我上传了用户个人资料照片,如堆栈中的流量所表示的那样(

I'm using Quickblox, I uploaded users profile pic as said in the stack over flow (How to upload the Users profile pic and how to fetch that profile pic from another users device?) It's working fine for me.

然后我可以在user2联系人中看到user1个人资料图片.(通过下载user1的blob文件并将其转换为图像)

Then I'm able to see the user1 profile pic in user2 contacts.(By downloading user1's blob file and converting it into image)

但是现在我的问题是,如果我们要使用user1和登录的user2创建一个新的聊天对话框.我应该将该用户1的个人资料图片(如上面的流程示例中的上载所示)链接到我将创建一个新对话框以与user1聊天.

But now my question is, if we want to create a new chat dialog with user1, and user2 as logged in. There I should link this user1's profile pic (which is uploaded as said in the above stack over flow example) with new dialog that I'm going to create to chat with user1.

问题:1-那么如何将该用户的个人资料图片与新创建的聊天对话框相关联,尤其是在一对一聊天(私人聊天)的情况下.

Question:1 - So How can I link this user's profile pic with newly created chat dialog especially in case of One to one chat.(Private chat).

问题:2-只要user1更改了个人资料照片(使用update blob api调用),聊天对话框中的图像就应该能够更新.正如我们在WhatsApp中看到的那样.

Question:2 -And that image in chat dialog should be able to update whenever user1 has changed his profile pic (using update blob api call). As we can see in WhatsApp.

推荐答案

在,

问题:1

如果我理解您的问题,当您为两个用户创建一个对话框时,您想在您的对话框列表中显示该对话框图像,并且该对话框图像应该是其他用户的图像,我的意思是user1应该将对话框图像视为User2并将User2应该看到对话框图像为User1正确???

If I understood your question, when you create a dialog for two user you want to show the dialog image in dialog list of yours and dialog image should be the image of other user correct I mean user1 should see dialog image as User2 and User2 should see dialog image as User1 correct ???

如果是,哥们对不起!但是您使用的方法是错误的:)对话框的属性对于两个用户都是相同的.因此,如果将对话框的图像设置为User1,则user2将看到User1图片,但User1也将看到他自己的图像,这是错误的:)

If yes, buddy sorry! but the approach you are following is wrong :) Dialog's property will be same for both the user's. So if you set the dialog's image as User1, user2 will see User1 pic but User1 will also see his own image which is wrong :)

我要解决的是,除非对话框是组对话框,否则不要为对话框本身使用任何图像或名称:

What I did to tackle is, dont use any image or name for the dialog itself unless it is a group dialog :)

每次您收到 QBChat 时,您都会在其中拥有乘员ID.如果是 QBChatPrivate 消息,乘员ID的计数将为2,其中之一将是您自己的ID.因此,删除该名称将为您提供其他用户的ID.

Everytime you receive a QBChat you will have occupant id's in it. In case of QBChatPrivatemessages occupant id's count will be 2 and one of them will be your own id. So removing that will give you the id of other user.

每次我收到一条消息时,我都会找到参与其中的参与者,而不是我本人.在将消息插入coredata并将其显示在应用程序中之前,我会检查coredata以确定该用户是否存在于本地数据库中.如果用户存在,我将获取有关该用户的所有信息,例如他的个人资料图片,姓名,所有内容都将在列表中显示他的个人资料图片和他的姓名.否则,我将使用其ID下载有关该用户的所有信息,然后将其插入db,最后将消息插入db.现在,当我显示它时,我将遵循相同的步骤,在对话框列表中显示用户的姓名,个人资料图片和最后一条消息.

Everytime I recieve a message I'll find out the participants involved in it other than myself, and before inserting message to coredata and showing it in app, I check my coredata to find if the user exists in my local db. If user exists I'll fetch all the info about the user like his profile pic, name and everything show his profile pic and his name in list. Else I'll download all the info about the user using his id and then insert it to db and finally insert the message to db. And now when I show it I'll follow the same procedure, show user's name his profile picture and last message in dialog list.

这给人的感觉是对话框显示了另一个用户名和他的个人资料图片.但是实际上,对话框从来没有任何私人对话框的图像或名称:)

This gives the feel like dialog is showing the other user name and his profile picture. But in reality dialog never had any image or name for private dialog :)

结论

在私人对话框的情况下,请勿将任何图像或名称保存到ABChatDialog本身:)

Dont save any image or name to the ABChatDialog itself in case of private dialog :)

问题:2

WhatsApp中的图像更新不仅涉及客户端逻辑,还包括来自服务器的支持.不幸的是,您没有服务器提供的支持,服务器会向所有客户端发送通知,通知您有关个人资料图片更改的信息.希望Quickblox能够尽快实施.

Image update in WhatsApp is not just involves client logic but also includes the support from server. Unfortunately you dont have such support from server where server sends out a notification to all the clients informing about profile pic changes. Let's hope Quickblox implements it soon.

解决方案

我们采用一种简单的方法来处理它,每次用户进入对话框列表屏幕时,我们将所有与当前可见对话框有关的所有用户带入,并为每个用户创建单独的NSOperations,并获取包括他的姓名在内的所有信息,对所有内容进行映像以使用户始终保持更新:)

We dealt it with a simple approach, Everytime user comes to the dialog list screen, we take all the users involved in all the dialogs that are currently visible and create a seperate NSOperations for each user and get all the info including his name, image everything to keep the user updated all the time :)

在我们的应用程序中,我们提供了用户也可以更改其名称的规定,因此对于我们而言,完全更新用户而不是仅更新其个人资料图片很重要:)

In our app we have provision where user can change his name as well, so it was important for us to update the user entirely and not just his profile pic :)

所有操作都在后台线程中运行,使主线程保持空闲状态,并在后台更新coredata.

all the operations run in background thread keeping main thread free and update the coredata in background.

将NSFetchedResultsController与coredata一起使用,使我们可以在用户名及其个人资料图片更新后立即对其进行更新.

Using NSFetchedResultsController with coredata allows us to update the username and his profile pic as soon as it is updated.

,当您打开特定的用户消息时,也会发生相同的过程.我们会在您打开特定消息后立即获取并更新该特定用户的信息.

and the same process will happen when you open a specific user message as well. we fetch and update the info for that specific user as soon as you open his messages.

这涉及到冗余的服务器调用,我们对此非常了解,但是这种方法可以使用户信息始终保持更新.

This involves redundant server calls and we are pretty much aware of it, but this approach keeps the user info updated all the time.

编辑根据您的要求,为您提供一些代码:)这是一个非常抽象的代码,并非完全可以正常工作,但是我认为它应该为您提供一个很好的开端:)

EDIT As per your request to provide you a little bit of code :) here is a very abstract not completely working code but I believe it should give you good head start :)

问题1:此代码使您可以了解如何在添加聊天内容之前先添加用户:)

Question 1: This code gives you idea of how to add user first before adding the chat itself :)

- (void)processMessage:(QBChatMessage *)message{
    // processMessage method is called in background thread so creating a background managed object context for the queue. You can make use of performBlock as well

    //once you recieve message check if dialog exists in your db or not
    NSManagedObjectContext *privateManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    [privateManagedObjectContext setParentContext:appdelegate.managedObjectContext];

    NSFetchRequest *request=[[NSFetchRequest alloc] initWithEntityName:@"Dialog"];
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"id == %@",message.dialogID];
    [request setPredicate:predicate];
    NSArray *results=[privateManagedObjectContext executeFetchRequest:request error:nil];

    if([results count]>0){
        //once dialog found for the chat
        Dialog *updatedDialog=(Dialog *)[results objectAtIndex:0];
        // involves is a one to many relation ship between user and dialog
        NSPredicate *userExistsPredicate=[NSPredicate predicateWithFormat:@"recipient_Id = %@",[NSNumber numberWithInteger:message.senderID]];
        NSArray *foundUserArray=[[updatedDialog.involves allObjects] filteredArrayUsingPredicate:userExistsPredicate];
        if([foundUserArray count] > 0){
            //user exists in db
            //check if its a duplicate chat sometime same chat comes back specially in group chat
            NSFetchRequest *request=[[NSFetchRequest alloc] initWithEntityName:@"Chats"];
            NSPredicate *predicate=[NSPredicate predicateWithFormat:@"chat_Id == %@",message.ID];
            [request setPredicate:predicate];
            NSArray *results=[privateManagedObjectContext executeFetchRequest:request error:nil];

            if([results count] ==0){
                //message not in db
                Chats *recievedChat=[NSEntityDescription insertNewObjectForEntityForName:@"Chats" inManagedObjectContext:privateManagedObjectContext];
                recievedChat.sender_Id=[NSNumber  numberWithInteger:message.senderID];
                recievedChat.date_sent=message.dateSent;
                recievedChat.chat_Id=message.ID;
                recievedChat.belongs=updatedDialog;
                recievedChat.message_Body=message.text;
                //populate the way you want it
                //commit background context use perform block for better performance
                NSError *updateDialogError = nil;
                [privateManagedObjectContext save:&updateDialogError];
                dispatch_async(dispatch_get_main_queue(), ^{
                    //commit main context
                    [appdelegate.managedObjectContext save:nil];
                });
            }
        }

        else{
            //fetch user not in db
            [QBRequest usersWithIDs:@[[NSNumber numberWithInteger:message.senderID]] page:nil
                       successBlock:^(QBResponse *response, QBGeneralResponsePage *page, NSArray *users) {
                           dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

                               NSManagedObjectContext *privateManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
                               [privateManagedObjectContext setParentContext:appdelegate.managedObjectContext];
                               NSFetchRequest *request=[[NSFetchRequest alloc] initWithEntityName:@"Dialog"];
                               NSPredicate *predicate=[NSPredicate predicateWithFormat:@"id == %@",message.dialogID];
                               [request setPredicate:predicate];
                               NSArray *results=[privateManagedObjectContext executeFetchRequest:request error:nil];
                               Dialog *updatedDialog=[results objectAtIndex:0];
                               NSMutableArray *occupants=[[NSMutableArray alloc] init];

                               //create user
                               for(QBUUser *user in users){
                                   if(user.ID != appdelegate.currentUser.ID){
                                       Recipient *recipient=[NSEntityDescription insertNewObjectForEntityForName:@"Recipient" inManagedObjectContext:privateManagedObjectContext];
                                       recipient.recipient_Name=user.fullName;
                                       recipient.recipient_Id=[NSNumber numberWithInteger:user.ID];

                                       [occupants addObject:recipient];
                                   }
                               }


                               NSMutableArray *newUsersArray=[NSMutableArray arrayWithArray:[updatedDialog.involves allObjects]];
                               [newUsersArray addObjectsFromArray:occupants];
                               updatedDialog.involves=[NSSet setWithArray:newUsersArray];

                               NSFetchRequest *chatRequest=[[NSFetchRequest alloc] initWithEntityName:@"Chats"];
                               NSPredicate *chatPredicate=[NSPredicate predicateWithFormat:@"chat_Id == %@",message.ID];
                               [chatRequest setPredicate:chatPredicate];
                               NSArray *chatResults=[privateManagedObjectContext executeFetchRequest:chatRequest error:nil];

                               //add chat
                               if([chatResults count] ==0){
                                   //add chats or messages
                                   Chats *recievedChat=[NSEntityDescription insertNewObjectForEntityForName:@"Chats" inManagedObjectContext:privateManagedObjectContext];
                                   recievedChat.sender_Id=[NSNumber  numberWithInteger:message.senderID];
                                   recievedChat.date_sent=message.dateSent;
                                   recievedChat.chat_Id=message.ID;
                                   recievedChat.belongs=updatedDialog;
                                   recievedChat.message_Body=message.text;

                               }

                               //save context
                               NSError *updateDialogError = nil;
                               [privateManagedObjectContext save:&updateDialogError];

                               dispatch_async(dispatch_get_main_queue(), ^{
                                   //commit main context
                                   [appdelegate.managedObjectContext save:nil];
                               });
                           });
                       }];
        }
    }
}

问题2 您不需要该代码:)阅读完本教程的内容后,您将了解NSOperation的工作原理:),然后只需在NSOperation内编写quickblox api即可为您下载图像:)

Question 2 You dont need code for that :) Once you go through tutorials I have stated you will understand how NSOperation works :) and then simply write quickblox api inside NSOperation to download image for you :)

希望我能阐明我的观点.编码愉快

I hope I made my point clear. Happy coding

这篇关于如何从用户的Blob ID向对话框提供照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆