EMMsgImageBubbleView.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // EMMsgImageBubbleView.m
  3. // ChatDemo-UI3.0
  4. //
  5. // Created by XieYajie on 2019/2/14.
  6. // Copyright © 2019 XieYajie. All rights reserved.
  7. //
  8. #import <SDWebImage/UIImageView+WebCache.h>
  9. #import "EMMsgImageBubbleView.h"
  10. #define kEMMsgImageDefaultSize 120
  11. #define kEMMsgImageMinWidth 50
  12. #define kEMMsgImageMaxWidth 120
  13. #define kEMMsgImageMaxHeight 260
  14. @implementation EMMsgImageBubbleView
  15. - (instancetype)initWithDirection:(EMMessageDirection)aDirection
  16. type:(EMMessageType)aType
  17. {
  18. self = [super initWithDirection:aDirection type:aType];
  19. if (self) {
  20. // self.layer.borderColor = [UIColor lightGrayColor].CGColor;
  21. // self.layer.borderWidth = 1;
  22. self.contentMode = UIViewContentModeScaleAspectFill;
  23. }
  24. return self;
  25. }
  26. #pragma mark - Private
  27. - (CGSize)_getImageSize:(CGSize)aSize
  28. {
  29. CGSize retSize = CGSizeMake(kEMMsgImageDefaultSize, kEMMsgImageDefaultSize);
  30. do {
  31. if (aSize.width == 0 || aSize.height == 0) {
  32. break;
  33. }
  34. NSInteger tmpWidth = aSize.width;
  35. if (aSize.width < kEMMsgImageMinWidth) {
  36. tmpWidth = kEMMsgImageMinWidth;
  37. }
  38. if (aSize.width > kEMMsgImageMaxWidth) {
  39. tmpWidth = kEMMsgImageMaxWidth;
  40. }
  41. NSInteger tmpHeight = tmpWidth / aSize.width * aSize.height;
  42. if (tmpHeight > kEMMsgImageMaxHeight) {
  43. tmpHeight = kEMMsgImageMaxHeight;
  44. }
  45. retSize.width = tmpWidth;
  46. retSize.height = tmpHeight;
  47. } while (0);
  48. return retSize;
  49. }
  50. - (void)setThumbnailImageWithLocalPath:(NSString *)aLocalPath
  51. remotePath:(NSString *)aRemotePath
  52. thumbImgSize:(CGSize)aThumbSize
  53. imgSize:(CGSize)aSize
  54. {
  55. UIImage *img = nil;
  56. if ([aLocalPath length] > 0) {
  57. img = [UIImage imageWithContentsOfFile:aLocalPath];
  58. }
  59. __weak typeof(self) weakself = self;
  60. void (^block)(CGSize aSize) = ^(CGSize aSize) {
  61. CGSize layoutSize = [weakself _getImageSize:aSize];
  62. [weakself mas_updateConstraints:^(MASConstraintMaker *make) {
  63. make.width.mas_equalTo(layoutSize.width);
  64. make.height.mas_equalTo(layoutSize.height);
  65. }];
  66. };
  67. CGSize size = aThumbSize;
  68. if (aThumbSize.width == 0 || aThumbSize.height == 0) {
  69. size = aSize;
  70. }
  71. if (img) {
  72. self.image = img;
  73. size = img.size;
  74. } else {
  75. BOOL isAutoDownloadThumbnail = ([EMClient sharedClient].options.isAutoDownloadThumbnail);
  76. if (isAutoDownloadThumbnail) {
  77. [self sd_setImageWithURL:[NSURL URLWithString:aRemotePath] placeholderImage:[UIImage imageNamed:@"msg_img_broken"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  78. // if (error) {
  79. // self.image = [UIImage imageNamed:@"msg_img_broken"];
  80. // }
  81. }];
  82. } else {
  83. self.image = [UIImage imageNamed:@"msg_img_broken"];
  84. }
  85. }
  86. block(size);
  87. }
  88. #pragma mark - Setter
  89. - (void)setModel:(EMMessageModel *)model
  90. {
  91. EMMessageType type = model.type;
  92. if (type == EMMessageTypeImage) {
  93. EMImageMessageBody *body = (EMImageMessageBody *)model.emModel.body;
  94. NSString *imgPath = body.thumbnailLocalPath;
  95. if ([imgPath length] == 0 && model.direction == EMMessageDirectionSend) {
  96. imgPath = body.localPath;
  97. }
  98. [self setThumbnailImageWithLocalPath:imgPath remotePath:body.thumbnailRemotePath thumbImgSize:body.thumbnailSize imgSize:body.size];
  99. }
  100. }
  101. @end