@@ -51,6 +51,9 @@ + (CGFloat)heightForMessage:(ChatModel *)currentChatmodel premodel:(ChatModel *)
51
51
// 语音
52
52
}else if (hashEqual (currentChatmodel.contenType , Content_Audio)){
53
53
54
+ height = 50 ;
55
+ // 验证是否群聊
56
+ [self groupChatConfig: currentChatmodel];
54
57
return currentChatmodel.messageHeight += currentChatmodel.shouldShowTime ? height + 50 : height + 15 ;
55
58
// 图片
56
59
}else if (hashEqual (currentChatmodel.contenType , Content_Picture)){
@@ -74,6 +77,8 @@ + (CGFloat)heightForMessage:(ChatModel *)currentChatmodel premodel:(ChatModel *)
74
77
}else {
75
78
height = 120 ;
76
79
}
80
+ // 验证是否群聊
81
+ [self groupChatConfig: currentChatmodel];
77
82
return currentChatmodel.messageHeight += currentChatmodel.shouldShowTime ? height + 50 : height + 15 ;
78
83
// 视频
79
84
}else if (hashEqual (currentChatmodel.contenType , Content_Video)){
@@ -102,90 +107,150 @@ + (void)groupChatConfig:(ChatModel *)chatModel
102
107
#pragma marl - 创建发送消息模型
103
108
+ (ChatModel *)creatMessageModel : (ChatModel *)config
104
109
{
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 ;
122
- return messageModel;
110
+ ChatModel * chatModel = [[ChatModel alloc ]init];
111
+ ChatContentModel *content = [[ChatContentModel alloc ]init];
112
+ chatModel.content = content;
113
+ chatModel.fromUserID = [Account account ].myUserID ;
114
+ chatModel.toUserID = config.toUserID ;
115
+ chatModel.messageType = @" normal" ;
116
+ chatModel.chatType = config.chatType ;
117
+ chatModel.deviceType = @" iOS" ;
118
+ chatModel.versionCode = TCP_VersionCode;
119
+ chatModel.byMyself = @1 ;
120
+ chatModel.isSend = @0 ;
121
+ chatModel.isRead = @0 ;
122
+ chatModel.isSending = @1 ;
123
+ chatModel.beatID = TCP_beatBody;
124
+ chatModel.fromPortrait = [Account account ].portrait ;
125
+ chatModel.toPortrait = config.toPortrait ;
126
+ chatModel.toNickName = config.toNickName ;
127
+ chatModel.groupID = config.groupID ;
128
+ chatModel.groupName = config.groupName ;
129
+ chatModel.noDisturb = config.noDisturb ;
130
+
131
+ return chatModel;
123
132
}
124
133
125
- #pragma mark - 创建聊天资源缓存
126
- + (void )creatLocalCacheSource : (ChatAlbumModel *)albumModel chat : (ChatModel *)chatModel
134
+ // 初始化文本消息模型
135
+ + (ChatModel *)initTextMessage : (NSString *)text config : (ChatModel *)config
136
+ {
137
+ ChatModel *textModel = [self creatMessageModel: config];
138
+ textModel.contenType = Content_Text;
139
+ textModel.content .text = text;
140
+ return textModel;
141
+ }
142
+ // 初始化语音消息模型
143
+ + (ChatModel *)initAudioMessage : (ChatAlbumModel *)audio config : (ChatModel *)config
127
144
{
145
+ ChatModel *audioModel = [self creatMessageModel: config];
146
+ audioModel.contenType = Content_Audio;
147
+ audioModel.content .seconds = audio.duration ;
148
+
128
149
NSString *basePath = nil ;
129
- if (hashEqual (chatModel .chatType , @" userChat" )) {
130
- basePath = [ChatCache_Path stringByAppendingPathComponent: chatModel .toUserID];
150
+ if (hashEqual (config .chatType , @" userChat" )) {
151
+ basePath = [ChatCache_Path stringByAppendingPathComponent: config .toUserID];
131
152
}else {
132
- basePath = [ChatCache_Path stringByAppendingPathComponent: chatModel .groupID];
153
+ basePath = [ChatCache_Path stringByAppendingPathComponent: config .groupID];
133
154
}
134
155
135
156
NSFileManager *manager = [NSFileManager defaultManager ];
136
157
BOOL exist = [manager fileExistsAtPath: basePath];
137
158
if (!exist) {
138
159
[manager createDirectoryAtPath: basePath withIntermediateDirectories: YES attributes: nil error: NULL ];
139
160
}
140
-
141
- // ////////////////资源缓存
142
- // 压缩比
143
- CGFloat compressScale = 1 ;
144
- NSData *smallAlbumData = nil ;
145
- NSData *albumData = nil ;
146
- // 用户选择了原图
147
- if (albumModel.isOrignal ) {
161
+ // 语音写入缓存
162
+ NSString *name = [NSString stringWithFormat: @" ChatAudio_%@ .mp3" ,audioModel.sendTime];
163
+ [audio.audioData writeToFile: [basePath stringByAppendingPathComponent: name] atomically: YES ];
164
+ return audioModel;
165
+ }
166
+ // 初始化图片消息模型
167
+ + (NSArray <ChatModel *> *)initPicMessage : (NSArray <ChatAlbumModel *> *)pics config : (ChatModel *)config
168
+ {
169
+ NSMutableArray *tmpArray = [NSMutableArray array ];
170
+ for (ChatAlbumModel *pic in pics) {
148
171
149
- // 压缩过的小图缓存 (用户界面展示,节省资源)
150
- if (albumModel.orignalData .length /1024.0 ) { // 小于3M的
151
-
152
- compressScale = 0.1 ; // 压缩10倍
153
- }else { // 大于3M
154
-
155
- compressScale = 0.05 ; // 压缩20倍
172
+ // 创建图片模型
173
+ ChatModel *picModel = [self creatMessageModel: config];
174
+ picModel.contenType = Content_Picture;
175
+ picModel.content .picSize = pic.picSize ;
176
+ picModel.content .fileName = pic.name ;
177
+ [tmpArray addObject: picModel];
178
+
179
+ NSString *basePath = nil ;
180
+ if (hashEqual (config.chatType , @" userChat" )) {
181
+ basePath = [ChatCache_Path stringByAppendingPathComponent: config.toUserID];
182
+ }else {
183
+ basePath = [ChatCache_Path stringByAppendingPathComponent: config.groupID];
156
184
}
157
- UIImage *image = [UIImage imageWithData: albumModel.orignalData];
158
- // 小图data
159
- smallAlbumData = UIImageJPEGRepresentation (image, compressScale);
160
- // 原图data
161
- albumData = albumModel.orignalData ;
162
185
163
- // 默认选择,未选择原图
164
- }else {
186
+ NSFileManager *manager = [NSFileManager defaultManager ];
187
+ BOOL exist = [manager fileExistsAtPath: basePath];
188
+ if (!exist) {
189
+ [manager createDirectoryAtPath: basePath withIntermediateDirectories: YES attributes: nil error: NULL ];
190
+ }
165
191
166
- // 压缩过的小图缓存 (用户界面展示,节省资源)
167
- if (albumModel.normalData .length /1024.0 ) { // 小于3M的
192
+ // ////////////////资源缓存
193
+ // 压缩比
194
+ CGFloat compressScale = 1 ;
195
+ NSData *smallAlbumData = nil ;
196
+ NSData *albumData = nil ;
197
+ // 用户选择了原图
198
+ if (pic.isOrignal ) {
199
+
200
+ // 压缩过的小图缓存 (用户界面展示,节省资源)
201
+ if (pic.orignalPicData .length /1024.0 /1024.0 < 3 ) { // 小于3M的
202
+
203
+ compressScale = 0.1 ; // 压缩10倍
204
+ }else { // 大于3M
205
+
206
+ compressScale = 0.05 ; // 压缩20倍
207
+ }
208
+ UIImage *image = [UIImage imageWithData: pic.orignalPicData];
209
+ // 小图data
210
+ smallAlbumData = UIImageJPEGRepresentation (image, compressScale);
211
+ // 原图data
212
+ albumData = pic.orignalPicData ;
213
+
214
+ // 默认选择,未选择原图
215
+ }else {
168
216
169
- compressScale = 0.1 ; // 压缩10倍
170
- }else { // 大于3M
217
+ // 压缩过的小图缓存 (用户界面展示,节省资源)
218
+ if (pic.normalPicData .length /1024.0 /1024.0 < 3 ) { // 小于3M的
219
+
220
+ compressScale = 0.1 ; // 压缩10倍
221
+ }else { // 大于3M
222
+
223
+ compressScale = 0.05 ; // 压缩20倍
224
+ }
171
225
172
- compressScale = 0.05 ; // 压缩20倍
226
+ UIImage *image = [UIImage imageWithData: pic.normalPicData];
227
+ // 小图data
228
+ smallAlbumData = UIImageJPEGRepresentation (image, compressScale);
229
+ // 原图data
230
+ albumData = pic.normalPicData ;
173
231
}
174
-
175
- UIImage *image = [UIImage imageWithData: albumModel.normalData];
176
- // 小图data
177
- smallAlbumData = UIImageJPEGRepresentation (image, compressScale);
178
- // 原图data
179
- albumData = albumModel.normalData ;
232
+ // 小图缓存路径
233
+ NSString *smallDetailPath = [basePath stringByAppendingPathComponent: [NSString stringWithFormat: @" %@ _%@ " ,@" small" ,pic.name]];
234
+ // 原图缓存路径
235
+ NSString *detailPath = [basePath stringByAppendingPathComponent: pic.name];
236
+ // 小图写入缓存
237
+ [smallAlbumData writeToFile: smallDetailPath atomically: YES ];
238
+ // 原图写入缓存
239
+ [albumData writeToFile: detailPath atomically: YES ];
180
240
}
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 ];
241
+ return tmpArray;
242
+ }
243
+ // 初始化视频消息模型
244
+ + (ChatModel *)initVideoMessage : (ChatAlbumModel *)video config : (ChatModel *)config
245
+ {
246
+ return nil ;
189
247
}
190
248
249
+
250
+
251
+
252
+
253
+
254
+
255
+
191
256
@end
0 commit comments