2016-09-22 8 views
0

私はステッカーを送ることができるチャットアプリに取り組んでいます。私がこれまで行ってきたことは、ステッカービューでステッカーをタップすると、選択したステッカーがすぐにチャットルームに送られます。今私はステッカーをタップするとポップアップするstickerPreviewViewを追加するように求められ、選択されたステッカーは最初にユーザーの応答を待っているstickerPreviewViewに表示されます。ユーザーが選択したステッカーをタップすると(そのステッカーイメージの付いたUIButton)、チャットルームに送信されます。私の質問は、私がチャットルームにステッカーを送る方法に主に示す関連コードの下で与えられたことをどうやって行うことができるかです。プレビューサブビューで選択したステッカー(画像)を表示するにはどうすればよいですか?

InputFunctionView

// This method called when I tap on a sticker 
- (void)selectedSticker:(NSString *)stickerURLString { 

    gstickerURLString = stickerURLString; // Added by Sanit - Pass on stickerURLString to global variable 


    // Added by Sanit - Initialize StickerPreviewView 
    _stickerPreviewView = [[UIImageView alloc] initWithFrame:CGRectMake(0, +180, FrameWidth, 120)]; 
    _stickerPreviewView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f]; 
    _stickerPreviewView.userInteractionEnabled = YES; 
    [self.superview addSubview:_stickerPreviewView]; // Added by Sanit - Use superview to add subview StickerPreviewView; using self to add the subview not working ? 



    // Added by Sanit - Initialize PreviewCancelButton 
    self.previewCancelButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    self.previewCancelButton.frame = CGRectMake(Main_Screen_Width-30, SpaceForItems, 25, 25); 
    [self.previewCancelButton setBackgroundImage:[UIImage imageNamed:@"btn_previewcancel.png"] forState:UIControlStateNormal]; 
    [self.previewCancelButton setBackgroundImage:[UIImage imageNamed:@"btn_previewcancel.png"] forState:UIControlStateHighlighted]; 
    [_stickerPreviewView addSubview: self.previewCancelButton]; 
    [self.previewCancelButton addTarget:self action:@selector(cancelStickerPreviewButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 



    // Added by Sanit - Initialize StickerPreviewButton 
    self.stickerPreviewButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    self.stickerPreviewButton.frame = CGRectMake(Main_Screen_Width/2, SpaceForItems, 60, 60); 
    [_stickerPreviewView addSubview: self.stickerPreviewButton]; 
    [self.stickerPreviewButton addTarget:self action:@selector(sendStickerPreviewButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; // Added by Sanit - StickerPreviewButton fires action 



    // How to add selected sticker to display on stickerPreviewView? 
    //??? Call method to show sticker on StickerPreviewView here - How? 


} 

// Added by Sanit - Method when cancelStickerPreviewButton pressed 
- (void)cancelStickerPreviewButtonPressed:(id)sender { 

    NSLog(@"cancel sticker preview"); 
    [self.previewCancelButton removeFromSuperview]; 
    [_stickerPreviewView removeFromSuperview]; 

} 



// Added by Sanit - Method called when StickerPreviewButton pressed 
- (void)sendStickerPreviewButtonPressed: (UIButton *)sender { 

    [self.delegate InputFunctionView:self sendSticker:gstickerURLString]; // Added by Sanit - Send off sticker to chatroom 
    [self cancelStickerPreviewButtonPressed:nil]; 

} 

//??? Added by Sanit - Method to show sticker on StickerPreviewView - How? 

ChatRoomVC

それは部屋をチャットするステッカーを送信する方法を参照してくださいChatRoomVCをチェック - 私が送信する方法を複製しようとしているところStickerPreviewViewへのステッカーですが、それほど簡単ではありません。私はここで助けが必要です。

- (void)InputFunctionView:(InputFunctionView *)funcView sendSticker:(NSString *)stickerURLString { 

    NSDictionary *dic = @{@"sticker" : stickerURLString, 
          @"type"  : @(MessageTypeSticker), 
          @"target"  : _targetAccout, 
          @"topic"  : _topic, 
          @"isGroupMessage"  : @"0"}; 
    if (_chatRoomType == ChatRoomTypeGroup) { 
     dic = @{@"sticker" : stickerURLString, 
       @"type"  : @(MessageTypeSticker), 
       @"target"  : _gid, //_targetAccout 
       @"topic"  : _topic, 
       @"isGroupMessage"  : @"1"}; 
    } 
    [self dealTheFunctionData:dic]; 
    [self.view endEditing:YES]; 
} 

- (void)dealTheFunctionData:(NSDictionary *)dic { 

    if ([ObjectManager isConnectedToInternet] && [[MQTTManager sharedInstance].client MQTTStatus]) { 
     [self.chatModel addMyMessage:dic]; 
     [self.chatTableView reloadData]; 
     [self tableViewScrollToBottom]; 

    } else { 

     if ([dic[@"type"] intValue] == 0) { 
      IFView.textViewInput.text = dic[@"strContent"]; 
     } 

     [ObjectManager showIsConnectedToInternetFailWithDuration:1.0 inView:self.view]; 
    } 

ChatModelView

- (void)addMyMessage:(NSDictionary *)dic { 

    BOOL isTempMessage=false; 
    BOOL isUsingTempMessage=false; 

    /*************** MQTT ***************/ 

    //Check message type 
    MessageType messageType = [dic[@"type"] integerValue]; 
    if ([[NSString stringWithFormat:@"%@", dic[@"isGroupMessage"]] isEqualToString:@"1"]) { 
     _ChatRoomUserType = YES; 
    } 

    NSString *messageString; 

    NSString *refTempId; 

    //"strContent" 
    if (messageType == MessageTypeText) { 
     messageString = dic[@"strContent"]; 

     //"picture" 
    } else if (messageType == MessageTypePicture) { 
     messageString = dic[@"picture"]; 

     refTempId=dic[@"refTempId"]; 
     if (refTempId!=nil) { 
      isUsingTempMessage=true; 
      NSLog(@"refTempId=%@",refTempId); 
     } 


     //"voice" 
    } else if (messageType == MessageTypeVoice) { 
     messageString = dic[@"voice"]; 

     //"sticker" 
    } else if (messageType == MessageTypeSticker) { 
     messageString = dic[@"sticker"]; 
     messageString = [messageString stringByReplacingOccurrencesOfString:kStickerImageURL withString:@""]; 
     //"video" 
    } else if (messageType == MessageTypeVideo) { 
     messageString = dic[@"video"]; 
    } 

    NSString *targetString = dic[@"target"]; 
    NSString *topicString = dic[@"topic"]; 

    NSString *tempId=[NSString stringWithFormat:@"%@",dic[@"tempId"]]; 

    if (![tempId isEqualToString:@"(null)"]) { 
     NSLog(@"Temp id is not empty"); 
     isTempMessage=true; 
    } 

    NSString *messageId = [[User sharedUser] generateDoubleMessageID]; 

    double timeStamp = [[NSDate date] timeIntervalSince1970] * 1000; 

    if (!isTempMessage) { 
    [self sendMessageToMQTTServer:messageString Target:targetString Topic:topicString MessageType:messageType MessageId:messageId TimeStamp:timeStamp]; 

    } 

    /* Layout */ 

    NSMutableDictionary *dataDic = [NSMutableDictionary dictionaryWithDictionary:dic]; 

    NSString *URLStr = @"http://img0.bdstatic.com/img/image/shouye/xinshouye/mingxing16.jpg"; 
    [dataDic setObject:@(MessageFromMe) forKey:@"from"]; 
    [dataDic setObject:[[NSDate date] description] forKey:@"strTime"]; 
    [dataDic setObject:@"Hello,Sister" forKey:@"strName"]; 
    [dataDic setObject:URLStr forKey:@"strIcon"]; 
    [dataDic setObject:@"" forKey:@"strMessageStatus"]; 

    MessageFrame *messageFrame = [[MessageFrame alloc] init]; 
    messageFrame.isGroupMessage = _isGroupChat; 
    Message *message = [[Message alloc] init]; 
    message.isSending = YES; 

    if (!isTempMessage) { 
     message.messageId = messageId; 
     message.isTempMessage=false; 
    } 

    else { 
     message.messageId = tempId; 
     message.isTempMessage=true; 
    } 

    message.showNotSendButton = NO; 
    message.delegate = _chatRoomVC; 
    [message setWithDict:dataDic]; 



    if (!isTempMessage) { 

    NSDictionary *[email protected]{@"Message":messageString,@"Target":targetString,@"MessageType":@(messageType),@"MessageId":messageId}; 
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),^{ 
      NSDictionary *messageDict=[Message dictionaryWithPropertiesOfObject:message]; 
      NSLog(@"messageDict=%@",messageDict); 

      // Save to temp DB 
      NSLog(@"message object=%@",message); 
      NSLog(@"mqDic=%@",mqDict); 
      NSLog(@"messageDic=%@",messageDict); 

      NSString *msgId=mqDict[@"MessageId"]; 
      NSDictionary *[email protected]{@"MessageId":msgId,@"Topic":topicString,@"MQDict":mqDict,@"MessageDict":messageDict,@"TimeStamp":[NSString stringWithFormat:@"%.0f",timeStamp]}; 
      if (![[FMDBManager sharedInstance] didTempMessageExistWithMessageId:msgId]) { 
       [[FMDBManager sharedInstance] saveTempMessage:tempMessage]; 
      } 
     }); 

     // Use tempId 
     if (isUsingTempMessage) { 

      NSLog(@"usingTempMessage"); 
      // Get path from tempMessage 
      NSLog(@"refTempId=%@",refTempId); 

      Message *tempMessage=[self queryMessage:refTempId]; 

      if (tempMessage!=nil) { 
       // set file path of message 
       NSString *filePath=tempMessage.picture; 
       message.filePath=filePath; 
      } 
     } 
    } 

    else { 


    } 

    BOOL isShowTime = [message minuteOffSetStart:previousTime end:dataDic[@"strTime"]]; 

    messageFrame.showTime = isShowTime; 
    messageFrame.showIconEnable = self.showIconEnable; 
    messageFrame.showSelfNameEnable = self.showSelfNameEnable; 
    messageFrame.showFriendsNameEnable = self.showFriendsNameEnable; 

    [messageFrame setMessage:message]; 


    /*************** Layout ***************/ 
    NSLog(@"isTempMessage=%d",isTempMessage); 

    if (isTempMessage) { 

     NSLog(@"message=%@",message); 
    } 

    if (isUsingTempMessage) { 

     NSLog(@"Replace temp message %@ on screen",refTempId); 
     [self replaceMessage:refTempId WithMessageFrame:messageFrame]; 
    } 

    else { 
     NSLog(@"Show message on screen"); 
     //Charles edit 8/8 
     MessageFrame *msgFrame = self.dataSource.lastObject; 
     previousTime = msgFrame.timeStampStr; 

     [self.dataSource addObject:messageFrame]; 
    } 
} 

enter image description here

答えて

0

方法はNSStringのような入力を受信するように、私はその下にこれを見つけた文字列を使用してstickerPreviewButtonする画像を設定することができる:

[self.stickerPreviewButton setImageForState:UIControlStateNormal withURL:[NSURL URLWithString:stickerURLString]]; 
0

は、この作業を行うには多くのアプローチがありますが、これらの二つは、プロの道になります。

1:カスタムデリゲートプロトコルを作成し、デリゲートメソッドでデータを渡します。

2:iOSブロックを使用してこのタスクを実行します。

あなたは簡単で簡単なアプローチを使用できます。

関連する問題