2016-09-22 4 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

해당 문자열을 이용하여 stickerPreviewButton 대한 이미지를 설정할 수 I 이하이 발견는 NSString로 입력을 접수
0

이 작업을 할 수있는 많은 방법이 있지만이 두 전문 방법이 될 것입니다.

1 : 사용자 지정 대리인 프로토콜을 만들고 대리인 메서드로 데이터를 전달합니다.

2 : iOS 블록을 사용하여이 작업을 수행하십시오.

쉽고 간단하게 생각하는 방법을 사용할 수 있습니다.

[self.stickerPreviewButton setImageForState:UIControlStateNormal withURL:[NSURL URLWithString:stickerURLString]]; 
: 방법 으로서는