88
99#import " ChatUtil.h"
1010#import " ChatModel.h"
11+ #import " ChatAlbumModel.h"
1112#import " MYCoreTextLabel.h"
1213
1314@implementation ChatUtil
@@ -54,6 +55,25 @@ + (CGFloat)heightForMessage:(ChatModel *)currentChatmodel premodel:(ChatModel *)
5455 // 图片
5556 }else if (hashEqual (currentChatmodel.contenType , Content_Picture)){
5657
58+ CGFloat picHeight = currentChatmodel.content .picSize .height ;
59+ CGFloat picWidth = currentChatmodel.content .picSize .width ;
60+ // 宽大于高
61+ if (picWidth > picHeight) {
62+
63+ // 极宽极低固定50高
64+ if (100 *(picHeight/picWidth)<=50 ) {
65+ height = 50 ;
66+ }else {
67+ height = 135 *(picHeight/picWidth);
68+ }
69+ // 宽小于高
70+ }else if (picWidth < picHeight){
71+
72+ height = 130 ;
73+ // 宽高相等
74+ }else {
75+ height = 120 ;
76+ }
5777 return currentChatmodel.messageHeight += currentChatmodel.shouldShowTime ? height + 50 : height + 15 ;
5878 // 视频
5979 }else if (hashEqual (currentChatmodel.contenType , Content_Video)){
@@ -80,26 +100,92 @@ + (void)groupChatConfig:(ChatModel *)chatModel
80100
81101
82102#pragma marl - 创建发送消息模型
83- + (ChatModel *)creatMessageModel
103+ + (ChatModel *)creatMessageModel : (ChatModel *) config
84104{
85- ChatModel *messageModel = [[ChatModel alloc ]init];
86- ChatContentModel *content = [[ChatContentModel alloc ]init];
87- messageModel.content = content;
88- messageModel.fromUserID = [Account account ].myUserID ;
89- messageModel.toUserID = nil ;
90- messageModel.messageType = @" normal" ;
91- messageModel.deviceType = @" iOS" ;
92- messageModel.versionCode = TCP_VersionCode;
93- messageModel.byMyself = @1 ;
94- messageModel.isSend = @1 ;
95- messageModel.isRead = @0 ;
96- messageModel.beatID = TCP_beatBody;
97- messageModel.fromPortrait = [Account account ].portrait ;
98- messageModel.toNickName = nil ;
99- messageModel.noDisturb = nil ;
100-
105+ ChatModel *messageModel = [[ChatModel alloc ]init];
106+ ChatContentModel *content = [[ChatContentModel alloc ]init];
107+ messageModel.content = content;
108+ messageModel.fromUserID = [Account account ].myUserID ;
109+ messageModel.toUserID = config.toUserID ;
110+ messageModel.messageType = @" normal" ;
111+ messageModel.chatType = config.chatType ;
112+ messageModel.deviceType = @" iOS" ;
113+ messageModel.versionCode = TCP_VersionCode;
114+ messageModel.byMyself = @1 ;
115+ messageModel.isSend = @0 ;
116+ messageModel.isRead = @0 ;
117+ messageModel.beatID = TCP_beatBody;
118+ messageModel.fromPortrait = [Account account ].portrait ;
119+ messageModel.toNickName = config.toNickName ;
120+ messageModel.groupID = config.groupID ;
121+ messageModel.noDisturb = config.noDisturb ;
101122 return messageModel;
102123}
103124
125+ #pragma mark - 创建聊天资源缓存
126+ + (void )creatLocalCacheSource : (ChatAlbumModel *)albumModel chat : (ChatModel *)chatModel
127+ {
128+ NSString *basePath = nil ;
129+ if (hashEqual (chatModel.chatType , @" userChat" )) {
130+ basePath = [ChatCache_Path stringByAppendingPathComponent: chatModel.toUserID];
131+ }else {
132+ basePath = [ChatCache_Path stringByAppendingPathComponent: chatModel.groupID];
133+ }
134+
135+ NSFileManager *manager = [NSFileManager defaultManager ];
136+ BOOL exist = [manager fileExistsAtPath: basePath];
137+ if (!exist) {
138+ [manager createDirectoryAtPath: basePath withIntermediateDirectories: YES attributes: nil error: NULL ];
139+ }
140+
141+ // ////////////////资源缓存
142+ // 压缩比
143+ CGFloat compressScale = 1 ;
144+ NSData *smallAlbumData = nil ;
145+ NSData *albumData = nil ;
146+ // 用户选择了原图
147+ if (albumModel.isOrignal ) {
148+
149+ // 压缩过的小图缓存 (用户界面展示,节省资源)
150+ if (albumModel.orignalData .length /1024.0 ) { // 小于3M的
151+
152+ compressScale = 0.1 ; // 压缩10倍
153+ }else { // 大于3M
154+
155+ compressScale = 0.05 ; // 压缩20倍
156+ }
157+ UIImage *image = [UIImage imageWithData: albumModel.orignalData];
158+ // 小图data
159+ smallAlbumData = UIImageJPEGRepresentation (image, compressScale);
160+ // 原图data
161+ albumData = albumModel.orignalData ;
162+
163+ // 默认选择,未选择原图
164+ }else {
165+
166+ // 压缩过的小图缓存 (用户界面展示,节省资源)
167+ if (albumModel.normalData .length /1024.0 ) { // 小于3M的
168+
169+ compressScale = 0.1 ; // 压缩10倍
170+ }else { // 大于3M
171+
172+ compressScale = 0.05 ; // 压缩20倍
173+ }
174+
175+ UIImage *image = [UIImage imageWithData: albumModel.normalData];
176+ // 小图data
177+ smallAlbumData = UIImageJPEGRepresentation (image, compressScale);
178+ // 原图data
179+ albumData = albumModel.normalData ;
180+ }
181+ // 小图缓存路径
182+ NSString *smallDetailPath = [basePath stringByAppendingPathComponent: [NSString stringWithFormat: @" %@ _%@ " ,@" small" ,albumModel.name]];
183+ // 原图缓存路径
184+ NSString *detailPath = [basePath stringByAppendingPathComponent: albumModel.name];
185+ // 小图写入缓存
186+ [smallAlbumData writeToFile: smallDetailPath atomically: YES ];
187+ // 原图写入缓存
188+ [albumData writeToFile: detailPath atomically: YES ];
189+ }
104190
105191@end
0 commit comments