123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- //
- // EMMsgImageBubbleView.m
- // ChatDemo-UI3.0
- //
- // Created by XieYajie on 2019/2/14.
- // Copyright © 2019 XieYajie. All rights reserved.
- //
- #import <SDWebImage/UIImageView+WebCache.h>
- #import "EMMsgImageBubbleView.h"
- #define kEMMsgImageDefaultSize 120
- #define kEMMsgImageMinWidth 50
- #define kEMMsgImageMaxWidth 120
- #define kEMMsgImageMaxHeight 260
- @implementation EMMsgImageBubbleView
- - (instancetype)initWithDirection:(EMMessageDirection)aDirection
- type:(EMMessageType)aType
- {
- self = [super initWithDirection:aDirection type:aType];
- if (self) {
- // self.layer.borderColor = [UIColor lightGrayColor].CGColor;
- // self.layer.borderWidth = 1;
- self.contentMode = UIViewContentModeScaleAspectFill;
- }
-
- return self;
- }
- #pragma mark - Private
- - (CGSize)_getImageSize:(CGSize)aSize
- {
- CGSize retSize = CGSizeMake(kEMMsgImageDefaultSize, kEMMsgImageDefaultSize);
- do {
- if (aSize.width == 0 || aSize.height == 0) {
- break;
- }
-
- NSInteger tmpWidth = aSize.width;
- if (aSize.width < kEMMsgImageMinWidth) {
- tmpWidth = kEMMsgImageMinWidth;
- }
- if (aSize.width > kEMMsgImageMaxWidth) {
- tmpWidth = kEMMsgImageMaxWidth;
- }
-
- NSInteger tmpHeight = tmpWidth / aSize.width * aSize.height;
- if (tmpHeight > kEMMsgImageMaxHeight) {
- tmpHeight = kEMMsgImageMaxHeight;
- }
-
- retSize.width = tmpWidth;
- retSize.height = tmpHeight;
-
- } while (0);
-
- return retSize;
- }
- - (void)setThumbnailImageWithLocalPath:(NSString *)aLocalPath
- remotePath:(NSString *)aRemotePath
- thumbImgSize:(CGSize)aThumbSize
- imgSize:(CGSize)aSize
- {
- UIImage *img = nil;
- if ([aLocalPath length] > 0) {
- img = [UIImage imageWithContentsOfFile:aLocalPath];
- }
-
- __weak typeof(self) weakself = self;
- void (^block)(CGSize aSize) = ^(CGSize aSize) {
- CGSize layoutSize = [weakself _getImageSize:aSize];
- [weakself mas_updateConstraints:^(MASConstraintMaker *make) {
- make.width.mas_equalTo(layoutSize.width);
- make.height.mas_equalTo(layoutSize.height);
- }];
- };
-
- CGSize size = aThumbSize;
- if (aThumbSize.width == 0 || aThumbSize.height == 0) {
- size = aSize;
- }
-
- if (img) {
- self.image = img;
- size = img.size;
- } else {
- BOOL isAutoDownloadThumbnail = ([EMClient sharedClient].options.isAutoDownloadThumbnail);
- if (isAutoDownloadThumbnail) {
- [self sd_setImageWithURL:[NSURL URLWithString:aRemotePath] placeholderImage:[UIImage imageNamed:@"msg_img_broken"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
- // if (error) {
- // self.image = [UIImage imageNamed:@"msg_img_broken"];
- // }
- }];
- } else {
- self.image = [UIImage imageNamed:@"msg_img_broken"];
- }
- }
-
- block(size);
- }
- #pragma mark - Setter
- - (void)setModel:(EMMessageModel *)model
- {
- EMMessageType type = model.type;
- if (type == EMMessageTypeImage) {
- EMImageMessageBody *body = (EMImageMessageBody *)model.emModel.body;
- NSString *imgPath = body.thumbnailLocalPath;
- if ([imgPath length] == 0 && model.direction == EMMessageDirectionSend) {
- imgPath = body.localPath;
- }
- [self setThumbnailImageWithLocalPath:imgPath remotePath:body.thumbnailRemotePath thumbImgSize:body.thumbnailSize imgSize:body.size];
- }
- }
- @end
|