JXEmojiCell.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. //
  2. // JXEmojiCell.m
  3. // shiku_im
  4. //
  5. // Created by JayLuo on 2019/12/13.
  6. // Copyright © 2019 Reese. All rights reserved.
  7. //
  8. #import "JXEmojiCell.h"
  9. #import "ImageBrowserViewController.h"
  10. #import "SCGIFImageView.h"
  11. #define SD_WEBP 1
  12. #import "UIImageView+WebCache.h"
  13. #import "UIImage+WebP.h"
  14. @implementation JXEmojiCell
  15. - (void)awakeFromNib {
  16. [super awakeFromNib];
  17. // Initialization code
  18. }
  19. - (void)dealloc{
  20. //[g_notify removeObserver:self name:kCellReadDelNotification object:self.msg];
  21. }
  22. -(void)creatUI{
  23. _chatImage=[[FLAnimatedImageView alloc]initWithFrame:CGRectZero];
  24. [_chatImage setBackgroundColor:[UIColor clearColor]];
  25. // _chatImage.layer.cornerRadius = 6;
  26. // _chatImage.layer.masksToBounds = YES;
  27. self.bubbleBg.backgroundColor = [UIColor clearColor];
  28. [self.bubbleBg addSubview:_chatImage];
  29. // [_chatImage release];
  30. _imageProgress = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 45, 45)];
  31. _imageProgress.center = CGPointMake(_chatImage.frame.size.width/2,_chatImage.frame.size.height/2);
  32. _imageProgress.layer.masksToBounds = YES;
  33. _imageProgress.layer.borderWidth = 2.f;
  34. _imageProgress.layer.borderColor = [UIColor whiteColor].CGColor;
  35. _imageProgress.layer.cornerRadius = _imageProgress.frame.size.width/2;
  36. _imageProgress.text = @"0%";
  37. _imageProgress.hidden = YES;
  38. _imageProgress.font = SYSFONT(13);
  39. _imageProgress.textAlignment = NSTextAlignmentCenter;
  40. _imageProgress.textColor = [UIColor whiteColor];
  41. [_chatImage addSubview:_imageProgress];
  42. }
  43. - (void)updateFileLoadProgress {
  44. dispatch_async(dispatch_get_main_queue(), ^{
  45. // UI更新代码
  46. if ([self.fileDict isEqualToString:self.msg.messageId]) {
  47. _imageProgress.hidden = NO;
  48. if (self.loadProgress >= 1) {
  49. _imageProgress.text = [NSString stringWithFormat:@"99%@",@"%"];
  50. }else {
  51. _imageProgress.text = [NSString stringWithFormat:@"%d%@",(int)(self.loadProgress*100),@"%"];
  52. }
  53. }
  54. // if (self.loadProgress >= 1) {
  55. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  56. // _imageProgress.hidden = YES;
  57. // });
  58. // }
  59. });
  60. }
  61. - (void)sendMessageToUser {
  62. _imageProgress.text = [NSString stringWithFormat:@"100%@",@"%"];
  63. _imageProgress.hidden = YES;
  64. }
  65. -(void)setCellData{
  66. [super setCellData];
  67. [self setUIFrame];
  68. [g_notify addObserver:self selector:@selector(imageDidDismiss:) name:kImageDidTouchEndNotification object:nil];
  69. }
  70. - (void)setUIFrame{
  71. NSURL* url;
  72. if(self.msg.isMySend && isFileExist(self.msg.fileName))
  73. url = [NSURL fileURLWithPath:self.msg.fileName];
  74. else
  75. url = [NSURL URLWithString:[self.msg.content stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  76. [self setCellSubViewFrame:CGSizeMake(200, imageItemHeight)];
  77. self.chatImage.image = [UIImage imageNamed:@"Default_Gray"];
  78. if ([url.absoluteString rangeOfString:@".gif"].location != NSNotFound) {
  79. [self loadAnimatedImageWithURL:url completion:^(FLAnimatedImage *animatedImage) {
  80. self.chatImage.animatedImage = animatedImage;
  81. [self setCellSubViewFrame:_chatImage.image.size];
  82. [self setBackgroundImage];
  83. // self.wait.hidden = YES;
  84. // [self.wait stopAnimating];
  85. }];
  86. }else if ([url.absoluteString rangeOfString:@".webp"].location != NSNotFound){
  87. @try {
  88. NSData *data = [NSData dataWithContentsOfURL:url];
  89. //UIImage *img = [UIImage sd_imageWithWebPData:data];
  90. UIImage *img = [UIImage imageWithData:data];
  91. _chatImage.image = img;
  92. } @catch (NSException *exception) {
  93. } @finally {
  94. }
  95. [self setCellSubViewFrame:_chatImage.image.size];
  96. [self setBackgroundImage];
  97. }else {
  98. // self.wait.hidden = NO;
  99. // [self.wait startAnimating];
  100. [_chatImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Default_Gray"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  101. // _chatImage.image = image;
  102. [self setCellSubViewFrame:_chatImage.image.size];
  103. [self setBackgroundImage];
  104. // self.wait.hidden = YES;
  105. // [self.wait stopAnimating];
  106. }];
  107. }
  108. if ([self.msg.isReadDel boolValue]) {
  109. _chatImage.alpha = 0.1;
  110. }else {
  111. _chatImage.alpha = 1;
  112. }
  113. }
  114. - (void)loadAnimatedImageWithURL:(NSURL *const)url completion:(void (^)(FLAnimatedImage *animatedImage))completion
  115. {
  116. NSString *const filename = url.lastPathComponent;
  117. NSString *const diskPath = [dataFilePath stringByAppendingPathComponent:filename];
  118. NSData * __block animatedImageData = [[NSFileManager defaultManager] contentsAtPath:diskPath];
  119. FLAnimatedImage * __block animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:animatedImageData];
  120. if (animatedImage) {
  121. if (completion) {
  122. completion(animatedImage);
  123. }
  124. } else {
  125. [[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  126. animatedImageData = data;
  127. animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:animatedImageData];
  128. if (animatedImage) {
  129. if (completion) {
  130. dispatch_async(dispatch_get_main_queue(), ^{
  131. completion(animatedImage);
  132. });
  133. }
  134. [data writeToFile:diskPath atomically:YES];
  135. }
  136. }] resume];
  137. }
  138. }
  139. - (void) setCellSubViewFrame:(CGSize)size {
  140. int n = imageItemHeight;
  141. //后设置imageview大小
  142. float w = size.width * kScreenWidthScale;
  143. float h = size.height;
  144. if (w <= 0 || h <= 0){
  145. w = n;
  146. h = n;
  147. }
  148. float k = w/(h/n);
  149. if(k+INSETS > JX_SCREEN_WIDTH - 80)//如果超出屏幕宽度
  150. k = JX_SCREEN_WIDTH-n-INSETS;
  151. if(self.msg.isMySend)
  152. {
  153. if(w != 0 && h != 0){
  154. // self.bubbleBg.frame=CGRectMake(JX_SCREEN_WIDTH-HEAD_SIZE-INSETS*4-k+CHAT_WIDTH_ICON+10, INSETS, INSETS+k, n+INSETS-4);
  155. self.bubbleBg.frame=CGRectMake(CGRectGetMinX(self.headImage.frame)-k-CHAT_WIDTH_ICON-INSETS, INSETS, INSETS+k, n+INSETS-4);
  156. // _chatImage.frame = CGRectMake(INSETS*0.2 , INSETS*0.3, k, n);
  157. _chatImage.frame = self.bubbleBg.bounds;
  158. }
  159. }
  160. else
  161. {
  162. // self.bubbleBg.frame=CGRectMake(CGRectGetMaxX(self.headImage.frame) + INSETS-CHAT_WIDTH_ICON, INSETS2(self.msg.isGroup), k+INSETS-5, n+INSETS -4);
  163. self.bubbleBg.frame=CGRectMake(CGRectGetMaxX(self.headImage.frame) + CHAT_WIDTH_ICON, INSETS2(self.msg.isGroup)+5, k+INSETS-5, n+INSETS -4);
  164. // self.chatImage.frame = CGRectMake(INSETS*0.5, INSETS*0.3, k, n);
  165. _chatImage.frame = self.bubbleBg.bounds;
  166. // _chatImage.contentMode = UIViewContentModeScaleAspectFit;
  167. /*
  168. // 如果超出屏幕宽度
  169. if(self.bubbleBg.frame.size.width > (JX_SCREEN_WIDTH - 80)){
  170. self.bubbleBg.frame = CGRectMake(self.bubbleBg.frame.origin.x, self.bubbleBg.frame.origin.y, JX_SCREEN_WIDTH - n, self.bubbleBg.frame.size.height);
  171. _chatImage.frame = CGRectMake(_chatImage.frame.origin.x, _chatImage.frame.origin.y, JX_SCREEN_WIDTH - n, _chatImage.frame.size.height);
  172. }
  173. */
  174. }
  175. if (self.msg.isShowTime) {
  176. CGRect frame = self.bubbleBg.frame;
  177. frame.origin.y = self.bubbleBg.frame.origin.y + 40;
  178. self.bubbleBg.frame = frame;
  179. }
  180. _imageProgress.center = CGPointMake(_chatImage.frame.size.width/2,_chatImage.frame.size.height/2);
  181. [self setMaskLayer:_chatImage];
  182. }
  183. -(void)didTouch:(UIButton*)button{
  184. NSLog(@"imageCell ------");
  185. [g_notify postNotificationName:kCellImageNotifaction object:@(self.indexNum)];
  186. if ([self.msg.isReadDel boolValue] && !self.msg.isMySend) {
  187. [self.msg sendAlreadyReadMsg];
  188. //阅后即焚图片通知
  189. [g_notify postNotificationName:kCellReadDelNotification object:self.msg];
  190. // [self.delegate performSelectorOnMainThread:self.readDele withObject:self.msg waitUntilDone:NO];
  191. _chatImage.alpha = 1;
  192. }
  193. // ImageBrowserViewController *imageVC = [ImageBrowserViewController sharedInstance];
  194. // imageVC.delegate = self;
  195. // imageVC.seeOK = @selector(imageDidDismiss);
  196. }
  197. - (void)timeGo:(JXMessageObject *)msg {
  198. if (self.msg.isMySend && [self.msg.isReadDel boolValue]) {
  199. [self deleteReadMsg];
  200. }
  201. }
  202. - (void)deleteReadMsg {
  203. if (![self.msg.isReadDel boolValue]) {
  204. return;
  205. }
  206. if(self.delegate != nil && [self.delegate respondsToSelector:self.readDele]){
  207. [UIView animateWithDuration:2.f animations:^{
  208. _chatImage.alpha = 0.0f;
  209. self.readImage.alpha = 0.f;
  210. self.burnImage.alpha = 0;
  211. } completion:^(BOOL finished) {
  212. [self.delegate performSelectorOnMainThread:self.readDele withObject:self.msg waitUntilDone:NO];
  213. _chatImage.alpha = 1.0f;
  214. self.readImage.alpha = 1.f;
  215. self.burnImage.alpha = 1.f;
  216. }];
  217. }
  218. }
  219. - (void)imageDidDismiss:(NSNotification *)notification{
  220. JXMessageObject *msg = notification.object;
  221. if ([msg.messageId isEqualToString:self.msg.messageId]) {
  222. if (![self.msg.isReadDel boolValue]) {
  223. return;
  224. }
  225. if (self.msg.isMySend) {
  226. if (!self.isRemove) {
  227. return;
  228. }
  229. }
  230. // if ([self.msg.isReadDel boolValue] && !self.msg.isMySend && self.isRemove) {
  231. if(self.delegate != nil && [self.delegate respondsToSelector:self.readDele]){
  232. //删除动画
  233. // NSString *path = [[NSBundle mainBundle]pathForResource:@"delete.gif" ofType:nil];
  234. // //NSData *gifData = [NSData dataWithContentsOfFile:path];
  235. // CGRect webFrame;
  236. // if(self.msg.isMySend){
  237. // CGFloat webW = webWidth-self.bubbleBg.frame.size.width;
  238. // webFrame = CGRectMake(self.bubbleBg.frame.origin.x - webW, self.bubbleBg.frame.origin.y, webWidth, self.frame.size.height - self.bubbleBg.frame.origin.y);
  239. // }else{
  240. // webFrame = CGRectMake(self.bubbleBg.frame.origin.x, self.bubbleBg.frame.origin.y, webWidth, self.frame.size.height - self.bubbleBg.frame.origin.y);
  241. // }
  242. // SCGIFImageView *gifImageView = [[SCGIFImageView alloc]initWithGIFFile:path];
  243. // gifImageView.frame = webFrame;
  244. // gifImageView.userInteractionEnabled = NO;
  245. // //UIWebView *webView = [[UIWebView alloc]initWithFrame:webFrame];
  246. // //webView.scalesPageToFit = YES;
  247. // //[webView loadData:gifData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
  248. // //webView.backgroundColor = [UIColor clearColor];
  249. // //webView.opaque = NO;
  250. // self.bubbleBg.hidden = YES;
  251. // [self addSubview:gifImageView];
  252. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  253. // [gifImageView removeFromSuperview];
  254. // [UIView animateWithDuration:2.f animations:^{
  255. // _chatImage.alpha = 0.0f;
  256. // } completion:^(BOOL finished) {
  257. // [self.delegate performSelectorOnMainThread:self.readDele withObject:self.msg waitUntilDone:NO];
  258. // _chatImage.alpha = 1.0f;
  259. // }];
  260. // self.bubbleBg.hidden = NO;
  261. // });
  262. }
  263. }
  264. // }
  265. }
  266. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  267. [super setSelected:selected animated:animated];
  268. // Configure the view for the selected state
  269. }
  270. -(int)getImageWidth{
  271. return [self.msg.location_x intValue];
  272. }
  273. -(int)getImageHeight{
  274. return [self.msg.location_y intValue];
  275. }
  276. + (float)getChatCellHeight:(JXMessageObject *)msg {
  277. if ([msg.chatMsgHeight floatValue] > 1) {
  278. return [msg.chatMsgHeight floatValue];
  279. }
  280. float n = 0;
  281. if (msg.isGroup && !msg.isMySend) {
  282. if (msg.isShowTime) {
  283. n = imageItemHeight+20*2 + 40;
  284. }else {
  285. n = imageItemHeight+20*2;
  286. }
  287. }else {
  288. if (msg.isShowTime) {
  289. n = imageItemHeight+10*2 + 40;
  290. }else {
  291. n = imageItemHeight+10*2;
  292. }
  293. }
  294. msg.chatMsgHeight = [NSString stringWithFormat:@"%f",n];
  295. if (!msg.isNotUpdateHeight) {
  296. [msg updateChatMsgHeight];
  297. }
  298. return n;
  299. }
  300. @end