QMChatRoomImageCell.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // QMChatRoomImageCell.m
  3. // IMSDK-OC
  4. //
  5. // Created by HCF on 16/3/10.
  6. // Copyright © 2016年 HCF. All rights reserved.
  7. //
  8. #import "QMChatRoomImageCell.h"
  9. #import "QMTapGestureRecognizer.h"
  10. #import "QMChatRoomShowImageController.h"
  11. #import <QMLineSDK/QMLineSDK.h>
  12. /**
  13. 图片消息
  14. */
  15. @implementation QMChatRoomImageCell
  16. {
  17. UIImageView *_imageView;
  18. NSString *_messageId;
  19. }
  20. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  21. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  22. if (self) {
  23. [self createUI];
  24. }
  25. return self;
  26. }
  27. - (void)createUI {
  28. _imageView = [[UIImageView alloc] init];
  29. _imageView.userInteractionEnabled = YES;
  30. _imageView.contentMode = UIViewContentModeScaleAspectFit;
  31. _imageView.clipsToBounds = YES;
  32. [self.contentView addSubview:_imageView];
  33. UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressTapGesture:)];
  34. [_imageView addGestureRecognizer:longPressGesture];
  35. }
  36. - (void)setData:(CustomMessage *)message avater:(NSString *)avater {
  37. _messageId = message._id;
  38. self.message = message;
  39. [super setData:message avater:avater];
  40. if ([message.fromType isEqualToString:@"0"]) {
  41. NSString *filePath = [NSString stringWithFormat:@"%@/%@/%@",NSHomeDirectory(),@"Documents",message.message];
  42. _imageView.image = [UIImage imageWithContentsOfFile:filePath];
  43. _imageView.frame = CGRectMake(CGRectGetMinX(self.iconImage.frame)-5-120, CGRectGetMaxY(self.timeLabel.frame)+10, 120, 140);
  44. UIImageView *maskImageView = [[UIImageView alloc] initWithFrame:_imageView.frame];
  45. maskImageView.image = [[UIImage imageNamed:@"SenderTextNodeBkg"] stretchableImageWithLeftCapWidth:20 topCapHeight:16];
  46. CALayer *layer = maskImageView.layer;
  47. layer.frame = CGRectMake(0, 0, maskImageView.frame.size.width, maskImageView.frame.size.height);
  48. _imageView.layer.mask = layer;
  49. self.sendStatus.frame = CGRectMake(CGRectGetMinX(_imageView.frame)-25, CGRectGetMinY(_imageView.frame)+5, 20, 20);
  50. }else {
  51. [_imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?imageView2/0/w/200/interlace/1/q/80",message.message]]];
  52. _imageView.frame = CGRectMake(CGRectGetMaxX(self.iconImage.frame)+5, CGRectGetMaxY(self.timeLabel.frame)+5, 120, 140);
  53. UIImageView *maskImageView = [[UIImageView alloc] initWithFrame:_imageView.frame];
  54. maskImageView.image = [[UIImage imageNamed:@"ReceiverTextNodeBkg"] stretchableImageWithLeftCapWidth:20 topCapHeight:16];
  55. CALayer *layer = maskImageView.layer;
  56. layer.frame = CGRectMake(0, 0, maskImageView.frame.size.width, maskImageView.frame.size.height);
  57. _imageView.layer.mask = layer;
  58. }
  59. QMTapGestureRecognizer * tapPressGesture = [[QMTapGestureRecognizer alloc] initWithTarget:self action:@selector(imagePressGesture:)];
  60. tapPressGesture.picName = message.message;
  61. tapPressGesture.picType = message.fromType;
  62. tapPressGesture.image = _imageView.image;
  63. [_imageView addGestureRecognizer:tapPressGesture];
  64. }
  65. - (void)longPressTapGesture:(UILongPressGestureRecognizer *)sender {
  66. if (sender.state == UIGestureRecognizerStateBegan) {
  67. [self becomeFirstResponder];
  68. UIMenuController *menu = [UIMenuController sharedMenuController];
  69. UIMenuItem *removeMenu = [[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(removeMenu:)];
  70. [menu setMenuItems:[NSArray arrayWithObjects:removeMenu, nil]];
  71. [menu setTargetRect:_imageView.frame inView:self];
  72. [menu setMenuVisible:true animated:true];
  73. UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
  74. if ([window isKeyWindow] == NO) {
  75. [window becomeKeyWindow];
  76. [window makeKeyAndVisible];
  77. }
  78. }
  79. }
  80. - (BOOL)canBecomeFirstResponder {
  81. return YES;
  82. }
  83. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
  84. if (action == @selector(removeMenu:)) {
  85. return YES;
  86. }else {
  87. return NO;
  88. }
  89. }
  90. - (void)imagePressGesture:(QMTapGestureRecognizer *)gestureRecognizer {
  91. QMChatRoomShowImageController * showPicVC = [[QMChatRoomShowImageController alloc] init];
  92. showPicVC.picName = gestureRecognizer.picName;
  93. showPicVC.picType = gestureRecognizer.picType;
  94. showPicVC.image = gestureRecognizer.image;
  95. showPicVC.modalPresentationStyle = UIModalPresentationFullScreen;
  96. [RQ_SHARE_FUNCTION.topViewController presentViewController:showPicVC animated:NO completion:nil];
  97. }
  98. - (void)removeMenu:(id)sender {
  99. // 删除语音(只能删除本地数据库消息)
  100. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"title.prompt", nil) message:NSLocalizedString(@"title.statement", nil) preferredStyle:UIAlertControllerStyleAlert];
  101. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"button.cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  102. }];
  103. UIAlertAction *sureAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"button.sure", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  104. [QMConnect removeDataFromDataBase:_messageId];
  105. [[NSNotificationCenter defaultCenter] postNotificationName:CHATMSG_RELOAD object:nil];
  106. }];
  107. [alertController addAction:cancelAction];
  108. [alertController addAction:sureAction];
  109. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
  110. }
  111. - (void)awakeFromNib {
  112. [super awakeFromNib];
  113. }
  114. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  115. [super setSelected:selected animated:animated];
  116. // Configure the view for the selected state
  117. }
  118. @end