菜鸟教程小白 发表于 2022-12-12 11:12:25

ios - 如何从用户的 blob id 将照片提供给对话框?


                                            <p><p>我正在使用 Quickblox,我上传了用户个人资料图片,如堆栈溢出 (<a href="https://stackoverflow.com/questions/37043763/how-to-upload-the-users-profile-pic-and-how-to-fetch-that-profile-pic-from-anoth/37048260?noredirect=1#comment61651930_37048260" rel="noreferrer noopener nofollow">How to upload the Users profile pic and how to fetch that profile pic from another users device?</a>) 中所述,这对我来说工作正常。 </p>

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

<p>但现在我的问题是,如果我们想创建一个与 user1 和 user2 登录的新聊天对话框。我应该将这个 user1 的个人资料图片(如上面的堆栈溢出示例中所述上传)与我要创建的新对话框用于与 user1 聊天。 </p>

<p>问题:1 - 那么我如何才能将此用户的个人资料图片与新创建的聊天对话框链接,尤其是在一对一聊天的情况下。(私有(private)聊天)。</p>

<p>问题:2 - 聊天对话框中的图像应该能够在用户 1 更改他的个人资料图片时更新(使用更新 blob api 调用)。正如我们在 WhatsApp 中看到的那样。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在,</p>

<p><strong>问题:1</strong></p>

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

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

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

<p>每次您收到 <code>QBChat</code> 时,您都会在其中获得居住者 ID。在 <code>QBChatPrivate</code>messages 的情况下,占用者 id 的计数将为 2,其中一个将是您自己的 id。所以删除它会给你其他用户的ID。</p>

<p>每次我收到一条消息时,我都会找出除了我自己之外的参与者,在将消息插入 coredata 并在应用程序中显示之前,我会检查我的 coredata 以查找用户是否存在于我的本地数据库中。如果用户存在,我将获取有关该用户的所有信息,例如他的个人资料图片、姓名和所有显示他的个人资料图片和列表中的姓名的信息。
否则,我将使用他的 id 下载有关用户的所有信息,然后将其插入 db,最后将消息插入 db。现在,当我展示它时,我将按照相同的程序,在对话框列表中显示用户名、他的个人资料图片和最后一条消息。</p>

<p>这给人的感觉就像对话框正在显示另一个用户名和他的个人资料图片。但实际上对话从来没有任何私有(private)对话的图像或名称:)</p>

<p><strong>结论</strong></p>

<p>在私有(private)对话的情况下,不要将任何图像或名称保存到 ABChatDialog 本身:)</p>

<p><strong>问题:2</strong></p>

<p>WhatsApp中的图像更新不仅涉及客户端逻辑,还包括来自服务器的支持。不幸的是,服务器没有这样的支持,服务器向所有客户端发送通知,通知配置文件图片更改。让我们希望 Quickblox 尽快实现它。</p>

<p><strong>解决方案</strong></p>

<p>我们用一种简单的方法来处理它,每次用户来到对话框列表屏幕时,我们都会获取所有参与当前可见对话框的用户,并为每个用户创建一个单独的 NSOperations 并获取包括他的姓名在内的所有信息,图像所有内容,以使用户始终保持更新:)</p>

<p>在我们的应用程序中,我们提供了用户也可以更改其姓名的规定,因此我们必须完全更新用户而不仅仅是他的个人资料图片:)</p>

<p>所有操作都在后台线程中运行,保持主线程空闲并在后台更新核心数据。</p>

<p>将 NSFetchedResultsController 与 coredata 一起使用,我们可以在更新用户名和他的个人资料图片后立即更新。</p>

<p>当您打开特定用户消息时也会发生相同的过程。一旦您打开他的消息,我们就会获取并更新该特定用户的信息。</p>

<p>这涉及冗余的服务器调用,我们非常清楚这一点,但这种方法始终保持用户信息的更新。 </p>

<p><strong>编辑</strong>
根据您的要求,为您提供一些代码:) 这是一个非常抽象但不完全工作的代码,但我相信它应该会给您一个良好的开端:)</p>

<p><strong>问题 1:</strong>此代码让您了解如何在添加聊天本身之前先添加用户 :)</p>

<pre><code>- (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 = [ initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    ;

    NSFetchRequest *request=[ initWithEntityName:@&#34;Dialog&#34;];
    NSPredicate *predicate=;
    ;
    NSArray *results=;

    if(&gt;0){
      //once dialog found for the chat
      Dialog *updatedDialog=(Dialog *);
      // involves is a one to many relation ship between user and dialog
      NSPredicate *userExistsPredicate=];
      NSArray *foundUserArray=[ filteredArrayUsingPredicate:userExistsPredicate];
      if( &gt; 0){
            //user exists in db
            //check if its a duplicate chat sometime same chat comes back specially in group chat
            NSFetchRequest *request=[ initWithEntityName:@&#34;Chats&#34;];
            NSPredicate *predicate=;
            ;
            NSArray *results=;

            if( ==0){
                //message not in db
                Chats *recievedChat=;
                recievedChat.sender_Id=;
                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;
                ;
                dispatch_async(dispatch_get_main_queue(), ^{
                  //commit main context
                  ;
                });
            }
      }

      else{
            //fetch user not in db
            ] page:nil
                     successBlock:^(QBResponse *response, QBGeneralResponsePage *page, NSArray *users) {
                           dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

                               NSManagedObjectContext *privateManagedObjectContext = [ initWithConcurrencyType:NSPrivateQueueConcurrencyType];
                               ;
                               NSFetchRequest *request=[ initWithEntityName:@&#34;Dialog&#34;];
                               NSPredicate *predicate=;
                               ;
                               NSArray *results=;
                               Dialog *updatedDialog=;
                               NSMutableArray *occupants=[ init];

                               //create user
                               for(QBUUser *user in users){
                                 if(user.ID != appdelegate.currentUser.ID){
                                       Recipient *recipient=;
                                       recipient.recipient_Name=user.fullName;
                                       recipient.recipient_Id=;

                                       ;
                                 }
                               }


                               NSMutableArray *newUsersArray=];
                               ;
                               updatedDialog.involves=;

                               NSFetchRequest *chatRequest=[ initWithEntityName:@&#34;Chats&#34;];
                               NSPredicate *chatPredicate=;
                               ;
                               NSArray *chatResults=;

                               //add chat
                               if( ==0){
                                 //add chats or messages
                                 Chats *recievedChat=;
                                 recievedChat.sender_Id=;
                                 recievedChat.date_sent=message.dateSent;
                                 recievedChat.chat_Id=message.ID;
                                 recievedChat.belongs=updatedDialog;
                                 recievedChat.message_Body=message.text;

                               }

                               //save context
                               NSError *updateDialogError = nil;
                               ;

                               dispatch_async(dispatch_get_main_queue(), ^{
                                 //commit main context
                                 ;
                               });
                           });
                     }];
      }
    }
}
</code></pre>

<p><strong>问题 2</strong>
你不需要代码 :) 一旦你完成了我所说的教程,你就会明白 NSOperation 是如何工作的:) 然后只需在 NSOperation 中编写 quickblox api 来为你下载图像:)</p>

<p>我希望我的观点很清楚。快乐编码</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何从用户的 blob id 将照片提供给对话框?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37066109/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37066109/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何从用户的 blob id 将照片提供给对话框?