SanitLee SanitLee - 2 months ago 5
Objective-C Question

How do I show selected sticker (image) on preview subview?

I have been working on a chat app that can send sticker. What I have done so far is when I tap on a sticker on the sticker view, the selected sticker will be sent to chat room immediately. Now I was asked to add stickerPreviewView which pops up when I tap on a sticker and the selected sticker will be shown on stickerPreviewView first awaiting user response. If user tap on the selected sticker (UIButton with the sticker image on it), it will be sent to the chat room. My question is how I can do that given below related code that mostly shows to how to send a sticker to chat room.

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

Check ChatRoomVC to see how it sends a sticker to chat room - where I'm trying to replicate a way to send a sticker to StickerPreviewView but it's not that simple and I need help here.

- (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

Answer

As the method receives input as NSString, I found this below that can set image for the stickerPreviewButton using the string:

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