简体   繁体   English

在UITableviewCell中的气泡上显示图像

[英]Display image on bubble in UITableviewCell

How do I display an image in a UITableviewCell when a selection is made from images in the imagepicker (see code sample below) as a message bubble (see screenshot below)? 当从图像选择器中的图像(请参见下面的代码示例)中选择一个消息气泡(请参见下面的屏幕截图)时,如何在UITableviewCell显示图像?

I am making chat app with XMPPFramework. 我正在使用XMPPFramework制作聊天应用程序。 I have used the implementation from this Link to display a speech bubble in a "tableview" cell for receiving and sending messages. 我已使用此链接中的实现在“表格视图”单元中显示气泡,用于接收和发送消息。

When I select an image or video, I want to display that image like in other chat applications. 选择图像或视频时,我希望像其他聊天应用程序一样显示该图像。

Here is code for UITableviewCell for row: 以下是UITableviewCell行的UITableviewCell代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *s = (NSDictionary *) [messages objectAtIndex:indexPath.row];
NSLog(@"s:-%@",s);

static NSString *CellIdentifier = @"MessageCellIdentifier";

SMMessageViewTableCell *cell = (SMMessageViewTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    //cell = [[SMMessageViewTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] ;
    cell=[[SMMessageViewTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

NSString *sender = [s objectForKey:@"sender"];
NSString *message = [s objectForKey:@"msg"];
//NSString *time = [s objectForKey:@"time"];

CGSize  textSize = { 260.0, 10000.0 };

CGSize size = [message sizeWithFont:[UIFont boldSystemFontOfSize:13]
                  constrainedToSize:textSize
                      lineBreakMode:NSLineBreakByWordWrapping];

size.width += (padding/2);

cell.messageContentView.text = message;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.userInteractionEnabled = NO;
cell.backgroundColor=[UIColor clearColor];
UIImage *bgImage = nil;

if ([sender isEqualToString:@"you"])
{ // left aligned
    bgImage = [[UIImage imageNamed:@"aqua.png"] stretchableImageWithLeftCapWidth:24  topCapHeight:15];

    [cell.messageContentView setFrame:CGRectMake(360 - size.width - padding,
                                                 padding*2,
                                                 size.width,
                                                 size.height+10)];

    UIImageView *imgview=[[UIImageView alloc]initWithFrame:CGRectMake(360 - size.width -padding, padding*2, size.width, size.height+10)];
    imgview.image=image;

    [cell.messageContentView addSubview:imgview];


    [cell.bgImageView setFrame:CGRectMake(cell.messageContentView.frame.origin.x - padding/10,cell.messageContentView.frame.origin.y - padding/10,size.width+padding,size.height+padding)];
}
else
{
    bgImage = [[UIImage imageNamed:@"orange.png"] stretchableImageWithLeftCapWidth:24  topCapHeight:15];

    [cell.messageContentView setFrame:CGRectMake(padding, padding*2, size.width, size.height+10)];

    [cell.bgImageView setFrame:CGRectMake(cell.messageContentView.frame.origin.x - padding/10,cell.messageContentView.frame.origin.y - padding/10,size.width+padding,size.height+padding)];
}

cell.bgImageView.image = bgImage;
//cell.senderAndTimeLabel.text = [NSString stringWithFormat:@"%@ %@", sender, time];
cell.senderAndTimeLabel.text = [self GetDateAndTime];

return cell;
}

在此处输入图片说明

In your message object, introduce a flag to determine the message type. 在您的消息对象中,引入一个标志来确定消息类型。 Let's say you name it isText. 假设您将其命名为isText。 Set it YES for text but Set it NO for media messages (Which you can do in uiimagepicker delegate method. Now in your cellForRowAtIndexPath , you can check the media type. If it is 'NO', you should insert a uiimageview as a subview in the cell to display that image. Make sure to leave margins in the bounds of that subview so that it is properly enclosed in the speech bubble. To make sure the speech bubble adjusts its own size accordingly, Now you will have to edit your heightForRowAtIndexPath Method as well. In the first line your are getting the message object there, Check the flag again. If it is NO, then return height for cell to something like msgImage.frame.size.height+(Top margin to parent view)+ (bottom margin to parentview). Left and right margins will be fixed by centring this imageView to centre of it's parentview ie speech bubble. 为文本设置为YES,为媒体消息设置为NO(您可以在uiimagepicker委托方法中进行此操作。现在在cellForRowAtIndexPath ,您可以检查媒体类型。如果为'NO',则应在其中插入uiimageview作为子视图要显示该图像的单元格,请确保在该子视图的边界中留有边距,以便将其正确地封装在对话气泡中。要确保对话气泡相应地调整其自身大小,现在您必须编辑heightForRowAtIndexPath方法在第一行中,您将获得消息对象,请再次检查该标志。如果为否,则将单元格的高度返回至msgImage.frame.size.height +(父视图的顶部边距)+(底部通过将此imageView居中于其父视图的中心(即气泡),可以固定左右边缘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM