2
0

WFCUStickerCell.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // ImageCell.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/9/2.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUStickerCell.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "WFCUMediaMessageDownloader.h"
  11. #import <SDWebImage/SDWebImage.h>
  12. #import "WFCUUtilities.h"
  13. @interface WFCUStickerCell ()
  14. @property (nonatomic, strong)UIImageView *thumbnailView;
  15. @end
  16. @implementation WFCUStickerCell
  17. + (CGSize)sizeForClientArea:(WFCUMessageModel *)msgModel withViewWidth:(CGFloat)width {
  18. WFCCStickerMessageContent *imgContent = (WFCCStickerMessageContent *)msgModel.message.content;
  19. CGSize size = imgContent.size;
  20. if (size.height > width || size.width > width) {
  21. float scale = MIN(width/size.height, width/size.width);
  22. size = CGSizeMake(size.width * scale, size.height * scale);
  23. }
  24. return size;
  25. }
  26. - (void)superUpdateModel:(WFCUMessageModel *)model {
  27. [super setModel:model];
  28. }
  29. - (void)setModel:(WFCUMessageModel *)model {
  30. WFCCStickerMessageContent *stickerMsg = (WFCCStickerMessageContent *)model.message.content;
  31. if (model.message.conversation.type == SecretChat_Type && model.message.direction == MessageDirection_Receive && model.message.status != Message_Status_Played) {
  32. [[WFCCIMService sharedWFCIMService] setMediaMessagePlayed:model.message.messageId];
  33. model.message.status = Message_Status_Played;
  34. }
  35. __weak typeof(self) weakSelf = self;
  36. if (!stickerMsg.localPath.length || ![WFCUUtilities isFileExist:stickerMsg.localPath]) {
  37. BOOL downloading = [[WFCUMediaMessageDownloader sharedDownloader] tryDownload:model.message success:^(long long messageUid, NSString *localPath) {
  38. if (messageUid == weakSelf.model.message.messageUid) {
  39. weakSelf.model.mediaDownloading = NO;
  40. stickerMsg.localPath = localPath;
  41. [weakSelf setModel:weakSelf.model];
  42. }
  43. } error:^(long long messageUid, int error_code) {
  44. if (messageUid == weakSelf.model.message.messageUid) {
  45. weakSelf.model.mediaDownloading = NO;
  46. [weakSelf superUpdateModel:weakSelf.model];
  47. }
  48. }];
  49. if (downloading) {
  50. model.mediaDownloading = YES;
  51. }
  52. } else {
  53. model.mediaDownloading = NO;
  54. }
  55. [super setModel:model];
  56. self.thumbnailView.frame = self.bubbleView.bounds;
  57. if (stickerMsg.localPath.length && [WFCUUtilities isFileExist:stickerMsg.localPath]) {
  58. if(model.message.conversation.type == SecretChat_Type && model.message.direction == MessageDirection_Receive) {
  59. NSData *data = [NSData dataWithContentsOfFile:stickerMsg.localPath];
  60. data = [[WFCCIMService sharedWFCIMService] decodeSecretChat:model.message.conversation.target mediaData:data];
  61. self.thumbnailView.image = [UIImage imageWithData:data];
  62. } else {
  63. [self.thumbnailView sd_setImageWithURL:[NSURL fileURLWithPath:stickerMsg.localPath]];
  64. }
  65. } else {
  66. self.thumbnailView.image = nil;
  67. }
  68. self.bubbleView.image = nil;
  69. }
  70. - (UIImageView *)thumbnailView {
  71. if (!_thumbnailView) {
  72. _thumbnailView = [[UIImageView alloc] init];
  73. [self.bubbleView addSubview:_thumbnailView];
  74. }
  75. return _thumbnailView;
  76. }
  77. @end