如何在预览子视图中显示所选贴纸(图像)?

时间:2016-09-22 15:03:14

标签: ios objective-c chat nsdictionary

我一直致力于可以发送贴纸的聊天应用。到目前为止我所做的是当我点击贴纸视图上的贴纸时,所选贴纸将立即发送到聊天室。现在我被要求添加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 *mqDict=@{@"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 *tempMessage=@{@"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

2 个答案:

答案 0 :(得分:0)

有很多方法可以完成这项任务,但这两种方法都是专业的。

1:创建自定义委托协议并使用委托方法传递数据。

2:使用iOS块执行此任务。

您可以使用您认为简单易行的任何方法。

答案 1 :(得分:0)

当方法接收输入为NSString时,我发现下面的内容可以使用字符串为stickerPreviewButton设置图像:

[self.stickerPreviewButton setImageForState:UIControlStateNormal withURL:[NSURL URLWithString:stickerURLString]];
相关问题