Skip to content

Commit b9d6d26

Browse files
committed
增加表情信息的发送,以及视频消息数据层的处理
1 parent 402c1d5 commit b9d6d26

24 files changed

Lines changed: 235 additions & 37 deletions

File tree

Example/MessageDisplayKitLeanchatExample/MessageDisplayKitLeanchatExample.xcodeproj/project.pbxproj

Lines changed: 86 additions & 4 deletions
Large diffs are not rendered by default.

Example/MessageDisplayKitLeanchatExample/MessageDisplayKitLeanchatExample/Sections/Common/LeanChatManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import <Foundation/Foundation.h>
1010
#import <AVOSCloud/AVOSCloud.h>
1111
#import <AVOSCloudIM/AVOSCloudIM.h>
12+
#import "AVIMEmotionMessage.h"
1213

1314
#define kJackClientID @"Jack"
1415
#define kDarcyClientID @"Darcy"
@@ -28,6 +29,8 @@ typedef void(^DidReceiveTypedMessageBlock)(AVIMTypedMessage *message);
2829

2930
+ (instancetype)manager;
3031

32+
-(NSString*)selfClientID;
33+
3134
- (void)setupDidReceiveCommonMessageCompletion:(DidReceiveCommonMessageBlock)didReceiveCommonMessageCompletion;
3235
- (void)setupDidReceiveTypedMessageCompletion:(DidReceiveTypedMessageBlock)didReceiveTypedMessageCompletion;
3336

Example/MessageDisplayKitLeanchatExample/MessageDisplayKitLeanchatExample/Sections/Common/LeanChatManager.m

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
@interface LeanChatManager () <AVIMClientDelegate>
1515

16-
@property (nonatomic, strong) AVIMClient *learnClient;
16+
@property (nonatomic, strong) AVIMClient *leanClient;
1717

1818
@property (nonatomic, copy) NSString *selfClientID;
1919

@@ -45,8 +45,8 @@ + (instancetype)manager {
4545
}
4646

4747
- (void)setup {
48-
self.learnClient = [[AVIMClient alloc] init];
49-
self.learnClient.delegate = self;
48+
self.leanClient = [[AVIMClient alloc] init];
49+
self.leanClient.delegate = self;
5050
}
5151

5252
- (instancetype)init {
@@ -70,11 +70,11 @@ - (void)setupDidReceiveTypedMessageCompletion:(DidReceiveTypedMessageBlock)didRe
7070
- (void)openSessionWithClientID:(NSString *)clientID
7171
completion:(void (^)(BOOL succeeded, NSError *error))completion {
7272
self.selfClientID = clientID;
73-
if (self.learnClient.status == AVIMClientStatusNone) {
74-
[self.learnClient openWithClientId:clientID callback:completion];
73+
if (self.leanClient.status == AVIMClientStatusNone) {
74+
[self.leanClient openWithClientId:clientID callback:completion];
7575
} else {
76-
[self.learnClient closeWithCallback:^(BOOL succeeded, NSError *error) {
77-
[self.learnClient openWithClientId:clientID callback:completion];
76+
[self.leanClient closeWithCallback:^(BOOL succeeded, NSError *error) {
77+
[self.leanClient openWithClientId:clientID callback:completion];
7878
}];
7979
}
8080
}
@@ -90,7 +90,7 @@ - (void)createConversationsWithClientIDs:(NSArray *)clientIDs
9090
- (void)createConversationsOnClientIDs:(NSArray *)clientIDs
9191
conversationType:(int)conversationType
9292
completion:(void (^)(BOOL, AVIMConversation *))completion {
93-
AVIMConversationQuery *query = [self.learnClient conversationQuery];
93+
AVIMConversationQuery *query = [self.leanClient conversationQuery];
9494
NSMutableArray *queryClientIDs = [[NSMutableArray alloc] initWithArray:clientIDs];
9595
[queryClientIDs insertObject:self.selfClientID atIndex:0];
9696
[query whereKey:kAVIMKeyMember containsAllObjectsInArray:queryClientIDs];
@@ -103,7 +103,7 @@ - (void)createConversationsOnClientIDs:(NSArray *)clientIDs
103103
}
104104
} else if (!objects || [objects count] < 1) {
105105
// 新建一个对话
106-
[self.learnClient createConversationWithName:nil
106+
[self.leanClient createConversationWithName:nil
107107
clientIds:queryClientIDs
108108
attributes:@{@"type":[NSNumber numberWithInt:conversationType]}
109109
options:AVIMConversationOptionNone
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// AVIMEmotionMessage.h
3+
// MessageDisplayKitLeanchatExample
4+
//
5+
// Created by lzw on 15/3/22.
6+
// Copyright (c) 2015年 iOS软件开发工程师 曾宪华 热衷于简洁的UI QQ:543413507 http://www.pailixiu.com/blog http://www.pailixiu.com/Jack/personal. All rights reserved.
7+
//
8+
9+
#import <AVOSCloudIM/AVOSCloudIM.h>
10+
11+
#define kAVIMMessageMediaTypeEmotion 1
12+
13+
@interface AVIMEmotionMessage : AVIMTypedMessage<AVIMTypedMessageSubclassing>
14+
15+
+ (instancetype)messageWithText:(NSString *)text
16+
attributes:(NSDictionary *)attributes;
17+
18+
@end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// AVIMEmotionMessage.m
3+
// MessageDisplayKitLeanchatExample
4+
//
5+
// Created by lzw on 15/3/22.
6+
// Copyright (c) 2015年 iOS软件开发工程师 曾宪华 热衷于简洁的UI QQ:543413507 http://www.pailixiu.com/blog http://www.pailixiu.com/Jack/personal. All rights reserved.
7+
//
8+
9+
#import "AVIMEmotionMessage.h"
10+
11+
@implementation AVIMEmotionMessage
12+
13+
+(void)load{
14+
[self registerSubclass];
15+
}
16+
17+
+ (AVIMMessageMediaType)classMediaType{
18+
return kAVIMMessageMediaTypeEmotion;
19+
}
20+
21+
+ (instancetype)messageWithText:(NSString *)text
22+
attributes:(NSDictionary *)attributes{
23+
AVIMEmotionMessage* message=[[self alloc] init];
24+
message.mediaType=[self classMediaType];
25+
message.text=text;
26+
message.attributes=attributes;
27+
return message;
28+
}
29+
30+
@end

Example/MessageDisplayKitLeanchatExample/MessageDisplayKitLeanchatExample/Sections/Pages/IM/LeanChatMessageTableViewController.m

Lines changed: 86 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ - (void)viewDidLoad
5959
// [self setBackgroundImage:[UIImage imageNamed:@"TableViewBackgroundImage"]];
6060

6161
// 设置自身用户名
62-
self.messageSender = kDarcyClientID;
62+
self.messageSender = [[LeanChatManager manager] selfClientID];
6363

6464
// 添加第三方接入数据
6565
NSMutableArray *shareMenuItems = [NSMutableArray array];
66-
NSArray *plugIcons = @[@"sharemore_pic", @"sharemore_video", @"sharemore_location", @"sharemore_friendcard", @"sharemore_myfav", @"sharemore_wxtalk", @"sharemore_videovoip", @"sharemore_voiceinput", @"sharemore_openapi", @"sharemore_openapi", @"avatar"];
67-
NSArray *plugTitle = @[@"照片", @"拍摄", @"位置", @"名片", @"我的收藏", @"实时对讲机", @"视频聊天", @"语音输入", @"大众点评", @"应用", @"曾宪华"];
66+
NSArray *plugIcons = @[@"sharemore_pic", @"sharemore_video",@"sharemore_location", @"sharemore_videovoip", @"sharemore_friendcard", @"sharemore_myfav", @"sharemore_wxtalk", @"sharemore_voiceinput", @"sharemore_openapi", @"sharemore_openapi", @"Avatar"];
67+
NSArray *plugTitle = @[@"照片", @"拍摄",@"位置",@"视频",@"名片", @"我的收藏", @"实时对讲机", @"语音输入", @"大众点评", @"应用", @"曾宪华"];
6868
for (NSString *plugIcon in plugIcons) {
6969
XHShareMenuItem *shareMenuItem = [[XHShareMenuItem alloc] initWithNormalIconImage:[UIImage imageNamed:plugIcon] title:[plugTitle objectAtIndex:[plugIcons indexOfObject:plugIcon]]];
7070
[shareMenuItems addObject:shareMenuItem];
@@ -78,7 +78,7 @@ - (void)viewDidLoad
7878
for (NSInteger j = 0; j < 18; j ++) {
7979
XHEmotion *emotion = [[XHEmotion alloc] init];
8080
NSString *imageName = [NSString stringWithFormat:@"section%ld_emotion%ld", (long)i , (long)j % 16];
81-
emotion.emotionPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"emotion%ld.gif", (long)j] ofType:@""];
81+
emotion.emotionPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"emotion%ld",(long)(j%16)] ofType:@"gif"];
8282
emotion.emotionConverPhoto = [UIImage imageNamed:imageName];
8383
[emotions addObject:emotion];
8484
}
@@ -103,7 +103,7 @@ - (void)viewDidLoad
103103
// 设置接收消息
104104
[[LeanChatManager manager] setupDidReceiveCommonMessageCompletion:^(AVIMMessage *message) {
105105
// 普通信息
106-
[self insertAVIMTypedMessage:message];
106+
//[self insertAVIMTypedMessage:message];
107107
}];
108108
[[LeanChatManager manager] setupDidReceiveTypedMessageCompletion:^(AVIMTypedMessage *message) {
109109
// 富文本信息
@@ -150,6 +150,43 @@ -(void)sendVoiceMessage:(XHMessage*)voiceMessage{;
150150
}];
151151
}
152152

153+
-(void)sendEmotionMessage:(XHMessage*)emotionMessage{
154+
AVFile* file=[AVFile fileWithName:@"emotion" contentsAtPath:emotionMessage.emotionPath];
155+
WEAKSELF
156+
[file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
157+
if(error==nil){
158+
AVIMEmotionMessage* sendEmotionMessage=[AVIMEmotionMessage messageWithText:file.url attributes:nil];
159+
[weakSelf.conversation sendMessage:sendEmotionMessage callback:^(BOOL succeeded, NSError *error) {
160+
DLog(@"succeed: %d, error:%@ ",succeeded,error);
161+
}];
162+
}else{
163+
DLog(@"error:%@",error);
164+
}
165+
}];
166+
}
167+
168+
-(void)sendVideoMessage:(XHMessage*)videoMessage{
169+
AVIMVideoMessage* sendVideoMessage=[AVIMVideoMessage messageWithText:nil attachedFilePath:videoMessage.videoPath attributes:nil];
170+
[self.conversation sendMessage:sendVideoMessage callback:^(BOOL succeeded, NSError *error) {
171+
DLog(@"succeed: %d, error:%@ ",succeeded,error);
172+
}];
173+
}
174+
175+
typedef void (^FetchFileBlock)(NSString* path,NSError* error);
176+
177+
-(void)fetchDataOfMessageFile:(AVFile*)file messageId:(NSString*)messageId completion:(FetchFileBlock)completion{
178+
NSString* path=[[NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingString:messageId];
179+
[file getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
180+
if(error){
181+
DLog(@"%@",error);
182+
completion(nil,error);
183+
}else{
184+
[data writeToFile:path atomically:YES];
185+
completion(path,error);
186+
}
187+
}];
188+
}
189+
153190
- (void)insertAVIMTypedMessage:(AVIMTypedMessage *)receiveMessage {
154191
AVIMMessageMediaType msgType = receiveMessage.mediaType;
155192
switch (msgType) {
@@ -176,25 +213,48 @@ - (void)insertAVIMTypedMessage:(AVIMTypedMessage *)receiveMessage {
176213
break;
177214
}
178215
case kAVIMMessageMediaTypeAudio:{
179-
AVIMAudioMessage* audioMessage=(AVIMAudioMessage*)receiveMessage;;
180-
NSDate* timestamp=[NSDate dateWithTimeIntervalSince1970:receiveMessage.sendTimestamp/1000];
181-
NSString* voicePath=[[NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingString:audioMessage.messageId];
182-
XHMessage* voiceMessage=[[XHMessage alloc] initWithVoicePath:voicePath voiceUrl:audioMessage.file.url voiceDuration:[NSString stringWithFormat:@"%.1f",audioMessage.duration] sender:audioMessage.clientId timestamp:timestamp];
183-
voiceMessage.bubbleMessageType = XHBubbleMessageTypeReceiving;
184-
voiceMessage.avatar = [UIImage imageNamed:@"Avatar"];
185-
voiceMessage.avatarUrl = @"http://www.pailixiu.com/jack/meIcon@2x.png";
186-
[self addMessage:voiceMessage];
187-
[self finishSendMessageWithBubbleMessageType:XHBubbleMessageMediaTypeVoice];
188-
NSFileManager* fileManager=[NSFileManager defaultManager];
189-
if([fileManager fileExistsAtPath:voicePath]==NO){
190-
[audioMessage.file getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
191-
if(error==nil){
192-
[data writeToFile:voicePath atomically:YES];
193-
}else{
194-
DLog(@"error:%@",error);
195-
}
196-
}];
197-
}
216+
WEAKSELF;
217+
[self fetchDataOfMessageFile:receiveMessage.file messageId:receiveMessage.messageId completion:^(NSString *path, NSError *error) {
218+
NSDate* timestamp=[NSDate dateWithTimeIntervalSince1970:receiveMessage.sendTimestamp/1000];
219+
AVIMAudioMessage* audioMessage=(AVIMAudioMessage*)receiveMessage;
220+
XHMessage* voiceMessage=[[XHMessage alloc] initWithVoicePath:path voiceUrl:nil voiceDuration:[NSString stringWithFormat:@"%.1f",audioMessage.duration] sender:receiveMessage.clientId timestamp:timestamp];
221+
voiceMessage.bubbleMessageType = XHBubbleMessageTypeReceiving;
222+
voiceMessage.avatar = [UIImage imageNamed:@"Avatar"];
223+
voiceMessage.avatarUrl = @"http://www.pailixiu.com/jack/meIcon@2x.png";
224+
[weakSelf addMessage:voiceMessage];
225+
[weakSelf finishSendMessageWithBubbleMessageType:XHBubbleMessageMediaTypeVoice];
226+
}];
227+
break;
228+
}
229+
case kAVIMMessageMediaTypeEmotion:{
230+
WEAKSELF
231+
AVFile* file=[AVFile fileWithURL:receiveMessage.text];
232+
[self fetchDataOfMessageFile:file messageId:receiveMessage.messageId completion:^(NSString *path, NSError *error) {
233+
NSDate* timestamp=[NSDate dateWithTimeIntervalSince1970:receiveMessage.sendTimestamp/1000];
234+
XHMessage *emotionMessage = [[XHMessage alloc] initWithEmotionPath:path sender:receiveMessage.clientId timestamp:timestamp];
235+
emotionMessage.bubbleMessageType = XHBubbleMessageTypeReceiving;
236+
emotionMessage.avatar = [UIImage imageNamed:@"Avatar"];
237+
emotionMessage.avatarUrl = @"http://www.pailixiu.com/jack/meIcon@2x.png";
238+
[weakSelf addMessage:emotionMessage];
239+
[weakSelf finishSendMessageWithBubbleMessageType:XHBubbleMessageMediaTypeEmotion];
240+
}];
241+
break;
242+
}
243+
case kAVIMMessageMediaTypeVideo:{
244+
WEAKSELF
245+
[self fetchDataOfMessageFile:receiveMessage.file messageId:receiveMessage.messageId completion:^(NSString *path, NSError *error) {
246+
UIImage *thumbnailImage;
247+
if(!error){
248+
thumbnailImage= [XHMessageVideoConverPhotoFactory videoConverPhotoWithVideoPath:path];
249+
}
250+
NSDate* timestamp=[NSDate dateWithTimeIntervalSince1970:receiveMessage.sendTimestamp/1000];
251+
XHMessage *videoMessage = [[XHMessage alloc] initWithVideoConverPhoto:thumbnailImage videoPath:path videoUrl:nil sender:receiveMessage.clientId timestamp:timestamp];
252+
videoMessage.bubbleMessageType = XHBubbleMessageTypeReceiving;
253+
videoMessage.avatar = [UIImage imageNamed:@"Avatar"];
254+
videoMessage.avatarUrl = @"http://www.pailixiu.com/jack/meIcon@2x.png";
255+
[weakSelf addMessage:videoMessage];
256+
[weakSelf finishSendMessageWithBubbleMessageType:XHBubbleMessageMediaTypeVideo];
257+
}];
198258
break;
199259
}
200260
default:
@@ -367,6 +427,7 @@ - (void)didSendVideoConverPhoto:(UIImage *)videoConverPhoto videoPath:(NSString
367427
videoMessage.avatarUrl = @"http://www.pailixiu.com/jack/meIcon@2x.png";
368428
[self addMessage:videoMessage];
369429
[self finishSendMessageWithBubbleMessageType:XHBubbleMessageMediaTypeVideo];
430+
[self sendVideoMessage:videoMessage];
370431
}
371432

372433
/**
@@ -399,6 +460,7 @@ - (void)didSendEmotion:(NSString *)emotionPath fromSender:(NSString *)sender onD
399460
emotionMessage.avatarUrl = @"http://www.pailixiu.com/jack/meIcon@2x.png";
400461
[self addMessage:emotionMessage];
401462
[self finishSendMessageWithBubbleMessageType:XHBubbleMessageMediaTypeEmotion];
463+
[self sendEmotionMessage:emotionMessage];
402464
}
403465

404466
/**
109 KB
Loading
87.2 KB
Loading
58.8 KB
Loading
94.9 KB
Loading

0 commit comments

Comments
 (0)