JXFaceCustomCell.m 13 KB

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