JXImageCell.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. //
  2. // JXImageCell.m
  3. // shiku_im
  4. //
  5. // Created by Apple on 16/10/10.
  6. // Copyright © 2016年 Reese. All rights reserved.
  7. //
  8. #import "JXImageCell.h"
  9. #import "ImageBrowserViewController.h"
  10. #import "SCGIFImageView.h"
  11. @implementation JXImageCell
  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. [self loadAnimatedImageWithURL:url completion:^(FLAnimatedImage *animatedImage) {
  77. self.chatImage.animatedImage = animatedImage;
  78. [self setCellSubViewFrame:_chatImage.image.size];
  79. [self setBackgroundImage];
  80. // self.wait.hidden = YES;
  81. // [self.wait stopAnimating];
  82. }];
  83. }else {
  84. // self.wait.hidden = NO;
  85. // [self.wait startAnimating];
  86. [_chatImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Default_Gray"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  87. // _chatImage.image = image;
  88. [self setCellSubViewFrame:_chatImage.image.size];
  89. [self setBackgroundImage];
  90. // self.wait.hidden = YES;
  91. // [self.wait stopAnimating];
  92. }];
  93. }
  94. if ([self.msg.isReadDel boolValue]) {
  95. _chatImage.alpha = 0.1;
  96. }else {
  97. _chatImage.alpha = 1;
  98. }
  99. }
  100. - (void)loadAnimatedImageWithURL:(NSURL *const)url completion:(void (^)(FLAnimatedImage *animatedImage))completion
  101. {
  102. NSString *const filename = url.lastPathComponent;
  103. NSString *const diskPath = [dataFilePath stringByAppendingPathComponent:filename];
  104. NSData * __block animatedImageData = [[NSFileManager defaultManager] contentsAtPath:diskPath];
  105. FLAnimatedImage * __block animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:animatedImageData];
  106. if (animatedImage) {
  107. if (completion) {
  108. completion(animatedImage);
  109. }
  110. } else {
  111. [[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  112. animatedImageData = data;
  113. animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:animatedImageData];
  114. if (animatedImage) {
  115. if (completion) {
  116. dispatch_async(dispatch_get_main_queue(), ^{
  117. completion(animatedImage);
  118. });
  119. }
  120. [data writeToFile:diskPath atomically:YES];
  121. }
  122. }] resume];
  123. }
  124. }
  125. - (void) setCellSubViewFrame:(CGSize)size {
  126. int n = imageItemHeight;
  127. //后设置imageview大小
  128. float w = size.width * kScreenWidthScale;
  129. float h = size.height;
  130. if (w <= 0 || h <= 0){
  131. w = n;
  132. h = n;
  133. }
  134. float k = w/(h/n);
  135. if(k+INSETS > JX_SCREEN_WIDTH - 80)//如果超出屏幕宽度
  136. k = JX_SCREEN_WIDTH-n-INSETS;
  137. if(self.msg.isMySend)
  138. {
  139. if(w != 0 && h != 0){
  140. self.bubbleBg.frame=CGRectMake(CGRectGetMinX(self.headImage.frame)-k-CHAT_WIDTH_ICON-INSETS, INSETS, INSETS+k, n+INSETS-4);
  141. // _chatImage.frame = CGRectMake(INSETS*0.2 , INSETS*0.3, k, n);
  142. _chatImage.frame = self.bubbleBg.bounds;
  143. }
  144. }
  145. else
  146. {
  147. self.bubbleBg.frame=CGRectMake(CGRectGetMaxX(self.headImage.frame) + CHAT_WIDTH_ICON, INSETS2(self.msg.isGroup)+8, k+INSETS-5, n+INSETS -4);
  148. // self.chatImage.frame = CGRectMake(INSETS*0.5, INSETS*0.3, k, n);
  149. _chatImage.frame = self.bubbleBg.bounds;
  150. // _chatImage.contentMode = UIViewContentModeScaleAspectFit;
  151. /*
  152. // 如果超出屏幕宽度
  153. if(self.bubbleBg.frame.size.width > (JX_SCREEN_WIDTH - 80)){
  154. self.bubbleBg.frame = CGRectMake(self.bubbleBg.frame.origin.x, self.bubbleBg.frame.origin.y, JX_SCREEN_WIDTH - n, self.bubbleBg.frame.size.height);
  155. _chatImage.frame = CGRectMake(_chatImage.frame.origin.x, _chatImage.frame.origin.y, JX_SCREEN_WIDTH - n, _chatImage.frame.size.height);
  156. }
  157. */
  158. }
  159. if (self.msg.isShowTime) {
  160. CGRect frame = self.bubbleBg.frame;
  161. frame.origin.y = self.bubbleBg.frame.origin.y + 40;
  162. self.bubbleBg.frame = frame;
  163. }
  164. _imageProgress.center = CGPointMake(_chatImage.frame.size.width/2,_chatImage.frame.size.height/2);
  165. [self setMaskLayer:_chatImage];
  166. }
  167. -(void)didTouch:(UIButton*)button{
  168. NSLog(@"imageCell ------");
  169. [g_notify postNotificationName:kCellImageNotifaction object:@(self.indexNum)];
  170. if ([self.msg.isReadDel boolValue] && !self.msg.isMySend) {
  171. [self.msg sendAlreadyReadMsg];
  172. //阅后即焚图片通知
  173. [g_notify postNotificationName:kCellReadDelNotification object:self.msg];
  174. // [self.delegate performSelectorOnMainThread:self.readDele withObject:self.msg waitUntilDone:NO];
  175. _chatImage.alpha = 1;
  176. }
  177. // ImageBrowserViewController *imageVC = [ImageBrowserViewController sharedInstance];
  178. // imageVC.delegate = self;
  179. // imageVC.seeOK = @selector(imageDidDismiss);
  180. }
  181. - (void)timeGo:(JXMessageObject *)msg {
  182. if (self.msg.isMySend && [self.msg.isReadDel boolValue]) {
  183. [self deleteReadMsg];
  184. }
  185. }
  186. - (void)deleteReadMsg {
  187. if (![self.msg.isReadDel boolValue]) {
  188. return;
  189. }
  190. if(self.delegate != nil && [self.delegate respondsToSelector:self.readDele]){
  191. [UIView animateWithDuration:2.f animations:^{
  192. _chatImage.alpha = 0.0f;
  193. self.readImage.alpha = 0.f;
  194. self.burnImage.alpha = 0;
  195. } completion:^(BOOL finished) {
  196. [self.delegate performSelectorOnMainThread:self.readDele withObject:self.msg waitUntilDone:NO];
  197. _chatImage.alpha = 1.0f;
  198. self.readImage.alpha = 1.f;
  199. self.burnImage.alpha = 1.f;
  200. }];
  201. }
  202. }
  203. - (void)imageDidDismiss:(NSNotification *)notification{
  204. JXMessageObject *msg = notification.object;
  205. if ([msg.messageId isEqualToString:self.msg.messageId]) {
  206. if (![self.msg.isReadDel boolValue]) {
  207. return;
  208. }
  209. if (self.msg.isMySend) {
  210. if (!self.isRemove) {
  211. return;
  212. }
  213. }
  214. // if ([self.msg.isReadDel boolValue] && !self.msg.isMySend && self.isRemove) {
  215. if(self.delegate != nil && [self.delegate respondsToSelector:self.readDele]){
  216. //删除动画
  217. // NSString *path = [[NSBundle mainBundle]pathForResource:@"delete.gif" ofType:nil];
  218. // //NSData *gifData = [NSData dataWithContentsOfFile:path];
  219. // CGRect webFrame;
  220. // if(self.msg.isMySend){
  221. // CGFloat webW = webWidth-self.bubbleBg.frame.size.width;
  222. // webFrame = CGRectMake(self.bubbleBg.frame.origin.x - webW, self.bubbleBg.frame.origin.y, webWidth, self.frame.size.height - self.bubbleBg.frame.origin.y);
  223. // }else{
  224. // webFrame = CGRectMake(self.bubbleBg.frame.origin.x, self.bubbleBg.frame.origin.y, webWidth, self.frame.size.height - self.bubbleBg.frame.origin.y);
  225. // }
  226. // SCGIFImageView *gifImageView = [[SCGIFImageView alloc]initWithGIFFile:path];
  227. // gifImageView.frame = webFrame;
  228. // gifImageView.userInteractionEnabled = NO;
  229. // //UIWebView *webView = [[UIWebView alloc]initWithFrame:webFrame];
  230. // //webView.scalesPageToFit = YES;
  231. // //[webView loadData:gifData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
  232. // //webView.backgroundColor = [UIColor clearColor];
  233. // //webView.opaque = NO;
  234. // self.bubbleBg.hidden = YES;
  235. // [self addSubview:gifImageView];
  236. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  237. // [gifImageView removeFromSuperview];
  238. // [UIView animateWithDuration:2.f animations:^{
  239. // _chatImage.alpha = 0.0f;
  240. // } completion:^(BOOL finished) {
  241. // [self.delegate performSelectorOnMainThread:self.readDele withObject:self.msg waitUntilDone:NO];
  242. // _chatImage.alpha = 1.0f;
  243. // }];
  244. // self.bubbleBg.hidden = NO;
  245. // });
  246. }
  247. }
  248. // }
  249. }
  250. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  251. [super setSelected:selected animated:animated];
  252. // Configure the view for the selected state
  253. }
  254. -(int)getImageWidth{
  255. return [self.msg.location_x intValue];
  256. }
  257. -(int)getImageHeight{
  258. return [self.msg.location_y intValue];
  259. }
  260. + (float)getChatCellHeight:(JXMessageObject *)msg {
  261. if ([msg.chatMsgHeight floatValue] > 1) {
  262. return [msg.chatMsgHeight floatValue];
  263. }
  264. float n = 0;
  265. if (msg.isGroup && !msg.isMySend) {
  266. if (msg.isShowTime) {
  267. n = imageItemHeight+20*2 + 40;
  268. }else {
  269. n = imageItemHeight+20*2;
  270. }
  271. n += GROUP_CHAT_INSET;
  272. }else {
  273. if (msg.isShowTime) {
  274. n = imageItemHeight+10*2 + 40;
  275. }else {
  276. n = imageItemHeight+10*2;
  277. }
  278. }
  279. msg.chatMsgHeight = [NSString stringWithFormat:@"%f",n];
  280. if (!msg.isNotUpdateHeight) {
  281. [msg updateChatMsgHeight];
  282. }
  283. return n;
  284. }
  285. @end