Skip to content

Commit f56f283

Browse files
author
mengyaoyao
committed
发送图片部分更新,逻辑完善
1 parent 79a6b56 commit f56f283

File tree

19 files changed

+481
-61
lines changed

19 files changed

+481
-61
lines changed

.DS_Store

0 Bytes
Binary file not shown.

CocoaAsyncSocket_TCP.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
B8661AA71ECD95680022753D /* ChatRecordTool.m in Sources */ = {isa = PBXBuildFile; fileRef = B8661AA61ECD95680022753D /* ChatRecordTool.m */; };
8282
B8661AAA1ECD998E0022753D /* UIImageView+GIF.m in Sources */ = {isa = PBXBuildFile; fileRef = B8661AA91ECD998E0022753D /* UIImageView+GIF.m */; };
8383
B89E046A1ED03F23000863F1 /* UIImage+photoPicker.m in Sources */ = {isa = PBXBuildFile; fileRef = B89E04691ED03F23000863F1 /* UIImage+photoPicker.m */; };
84+
B8EEC0C91ED47E2800F52E79 /* ChatConfigModel.m in Sources */ = {isa = PBXBuildFile; fileRef = B8EEC0C81ED47E2800F52E79 /* ChatConfigModel.m */; };
8485
E1527204E4A6C66E20ABAB19 /* Pods_CocoaAsyncSocket_TCP.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 24AA3076491D2596209CE424 /* Pods_CocoaAsyncSocket_TCP.framework */; };
8586
/* End PBXBuildFile section */
8687

@@ -252,6 +253,8 @@
252253
B8661AA91ECD998E0022753D /* UIImageView+GIF.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImageView+GIF.m"; sourceTree = "<group>"; };
253254
B89E04681ED03F23000863F1 /* UIImage+photoPicker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+photoPicker.h"; sourceTree = "<group>"; };
254255
B89E04691ED03F23000863F1 /* UIImage+photoPicker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+photoPicker.m"; sourceTree = "<group>"; };
256+
B8EEC0C71ED47E2800F52E79 /* ChatConfigModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ChatConfigModel.h; sourceTree = "<group>"; };
257+
B8EEC0C81ED47E2800F52E79 /* ChatConfigModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ChatConfigModel.m; sourceTree = "<group>"; };
255258
/* End PBXFileReference section */
256259

257260
/* Begin PBXFrameworksBuildPhase section */
@@ -643,6 +646,8 @@
643646
B85F3A8B1EC9C9E400B2EA9E /* ChatModel.m */,
644647
B816A1E61ED056070024859F /* ChatAlbumModel.h */,
645648
B816A1E71ED056070024859F /* ChatAlbumModel.m */,
649+
B8EEC0C71ED47E2800F52E79 /* ChatConfigModel.h */,
650+
B8EEC0C81ED47E2800F52E79 /* ChatConfigModel.m */,
646651
);
647652
path = ChatModel;
648653
sourceTree = "<group>";
@@ -917,6 +922,7 @@
917922
B816A1DA1ED040A20024859F /* TZImageManager.m in Sources */,
918923
B85F3A501EC9C9C600B2EA9E /* ChatNavigationController.m in Sources */,
919924
B816A1DE1ED040A20024859F /* TZPhotoPreviewCell.m in Sources */,
925+
B8EEC0C91ED47E2800F52E79 /* ChatConfigModel.m in Sources */,
920926
B85F3A4A1EC9C9C600B2EA9E /* NSString+extension.m in Sources */,
921927
B85F3A541EC9C9C600B2EA9E /* ReachState.m in Sources */,
922928
B816A1DC1ED040A20024859F /* TZImagePickerController.m in Sources */,

CocoaAsyncSocket_TCP/AppDelegate/AppDelegate.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
2424
//开启网络监听
2525
[GLobalRealReachability startNotifier];
2626

27+
//测试数据
28+
[Account account].myUserID = @"19910805";
29+
2730
return YES;
2831
}
2932

CocoaAsyncSocket_TCP/ChatHandler/ChatUtil.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88

99
#import <Foundation/Foundation.h>
1010

11-
@class ChatModel;
11+
@class ChatModel,ChatAlbumModel;
1212

1313
@interface ChatUtil : NSObject
1414

1515
//消息高度计算
1616
+ (CGFloat)heightForMessage:(ChatModel *)currentChatmodel premodel:(ChatModel *)premodel;
1717

1818
//创建消息模型
19-
+ (ChatModel *)creatMessageModel;
19+
+ (ChatModel *)creatMessageModel:(ChatModel *)config;
20+
21+
//创建本地资源缓存 , 图片写入本地
22+
+ (void)creatLocalCacheSource:(ChatAlbumModel *)albumModel chat:(ChatModel *)chatModel;
23+
2024
@end

CocoaAsyncSocket_TCP/ChatHandler/ChatUtil.m

Lines changed: 103 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
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

CocoaAsyncSocket_TCP/ChatModel/ChatAlbumModel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
@property (nonatomic, strong) NSData *normalData;
2121
//图片无压缩data
2222
@property (nonatomic, strong) NSData *orignalData;
23+
//图片尺寸
24+
@property (nonatomic, assign) CGSize picSize;
2325
//视频缓存地址
2426
@property (nonatomic, copy) NSString *videoCachePath;
2527
//视频缩略图
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// ChatConfigModel.h
3+
// CocoaAsyncSocket_TCP
4+
//
5+
// Created by 孟遥 on 2017/5/23.
6+
// Copyright © 2017年 mengyao. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface ChatConfigModel : NSObject
12+
13+
@property (nonatomic, copy) NSString *toUserID;
14+
@property (nonatomic, copy) NSString *nickName;
15+
@property (nonatomic, copy) NSString *groupID;
16+
@property (nonatomic, copy) NSString *groupName;
17+
@property (nonatomic, copy) NSString *chatType;
18+
19+
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// ChatConfigModel.m
3+
// CocoaAsyncSocket_TCP
4+
//
5+
// Created by 孟遥 on 2017/5/23.
6+
// Copyright © 2017年 mengyao. All rights reserved.
7+
//
8+
9+
#import "ChatConfigModel.h"
10+
11+
@implementation ChatConfigModel
12+
13+
@end

CocoaAsyncSocket_TCP/ChatModel/ChatModel.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ typedef NS_ENUM(NSInteger){
105105

106106
@property (nonatomic, strong) NSNumber *isSending; //是否正在发送中
107107

108+
@property (nonatomic, strong) NSNumber *progress; //进度
109+
108110
#pragma mark - chatlist独有部分
109111
@property (nonatomic, strong) NSNumber *unreadCount; //未读数
110112
@property (nonatomic, copy) NSString *lastMessage; //最后一条消息
@@ -124,6 +126,10 @@ typedef NS_ENUM(NSInteger){
124126

125127
@property (nonatomic, copy) NSString *text; //文本
126128

129+
@property (nonatomic, assign) CGSize picSize; //图片尺寸
130+
131+
@property (nonatomic, copy) NSString *fileName; //文件名
132+
127133
@property (nonatomic, strong) NSNumber *videoDuration; //语音时长
128134

129135
@property (nonatomic, copy) NSString *videoSize; //视频大小

CocoaAsyncSocket_TCP/Comon/Category/UIImage+photoPicker.m

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ + (void)openPhotoPickerGetImages:(photoPickerImagesCallback)imagesCallback targe
240240
NSString *name = [[info[@"PHImageFileSandboxExtensionTokenKey"]componentsSeparatedByString:@"/"]lastObject];
241241
imageModel.name = [NSString stringWithFormat:@"chatPicture_%@%@",getCurrentTime(),name];
242242
imageModel.orignalData = data;
243-
// imageModel.orignalImage = photo;
243+
imageModel.picSize = photo.size;
244244
imageModel.size = [@(data.length)stringValue];
245245

246246
if (index == assets.count - 1) {
@@ -257,19 +257,7 @@ + (void)openPhotoPickerGetImages:(photoPickerImagesCallback)imagesCallback targe
257257
}else{
258258

259259
__block int index = 0;
260-
261-
// ===============================================压缩新增
262260
NSArray *orignalImageArray = images;
263-
NSMutableArray *newSmallImagesArray = [NSMutableArray array];
264-
[orignalImageArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
265-
266-
//默认相册的照片压缩10倍 。iphone一张图压缩10倍一般也有几百K 基本上够用 ,
267-
UIImage *orignalImage = (UIImage *)obj;
268-
NSData *smallData = UIImageJPEGRepresentation(orignalImage, 0.1);
269-
UIImage *newSmallImage = [UIImage imageWithData:smallData];
270-
[newSmallImagesArray addObject:newSmallImage];
271-
}];
272-
// ================================================
273261

274262
NSMutableArray *imagesArray = [NSMutableArray array];
275263
[assets enumerateObjectsUsingBlock:^(PHAsset *_Nonnull asset, NSUInteger idx, BOOL * _Nonnull stop) {
@@ -284,13 +272,14 @@ + (void)openPhotoPickerGetImages:(photoPickerImagesCallback)imagesCallback targe
284272

285273
[[PHImageManager defaultManager]requestImageDataForAsset:asset options:option resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
286274

287-
// ============================压缩新增
275+
// ============================压缩
288276
NSData *normalData = UIImageJPEGRepresentation([UIImage imageWithData:imageData], 0.1);
289277
// ============================
290-
278+
UIImage *orignalImg = orignalImageArray[idx];
291279
NSString *name = [[info[@"PHImageFileSandboxExtensionTokenKey"]componentsSeparatedByString:@"/"]lastObject];
292280
imageModel.name = [NSString stringWithFormat:@"chatPicture_%@%@",getCurrentTime(),name];
293281
imageModel.normalData = normalData;
282+
imageModel.picSize = orignalImg.size;
294283
imageModel.size = [@(normalData.length)stringValue];
295284
//回调数据 2
296285
if (index == assets.count -1) {

CocoaAsyncSocket_TCP/Comon/Config.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,17 @@ View.layer.borderWidth = BorderWidth;
5454
#define UICOLOR_RGB_Alpha(_color,_alpha) [UIColor colorWithRed:((_color>>16)&0xff)/255.0f green:((_color>>8)&0xff)/255.0f blue:(_color&0xff)/255.0f alpha:_alpha]
5555
//分割线
5656
#define UILineColor UICOLOR_RGB_Alpha(0xe6e6e6,1)
57+
//主白色
5758
#define UIMainWhiteColor [UIColor whiteColor]
59+
//主背景色
60+
#define UIMainBackColor UICOLOR_RGB_Alpha(0xf0f0f0,1)
5861
//加载本地图片
5962
#define LoadImage(imageName) [UIImage imageNamed:imageName]
6063
//加载不缓存图片
6164
#define LoadImage_NotCache(imageName,imageType) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:imageName ofType:imageType]]
6265
//设置字体
6366
#define FontSet(fontSize) [UIFont systemFontOfSize:fontSize]
64-
67+
//聊天缓存基本地址 (根据当前用户来创建缓存目录 , 每个登录用户创建单独资源文件夹,每个会话创建单独的文件夹 , 便于管理)
68+
#define ChatCache_Path [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Library/Caches/ChatSource/%@",[Account account].myUserID]]
6569

6670
#endif /* Config_h */

CocoaAsyncSocket_TCP/Controller/ChatListViewController.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
103103
[tableView deselectRowAtIndexPath:indexPath animated:YES];
104104
ChatModel *seletedChatModel = self.messagesArray[indexPath.row];
105105
ChatViewController *chatVc = [[ChatViewController alloc]init];
106-
chatVc.chatModel = seletedChatModel;
106+
seletedChatModel.toUserID = @"123456";
107+
seletedChatModel.chatType = @"userChat";
108+
chatVc.config = seletedChatModel;
107109
[self.navigationController pushViewController:chatVc animated:YES];
108110
}
109111

CocoaAsyncSocket_TCP/Controller/ChatViewController.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@class ChatModel;
1212

1313
@interface ChatViewController : BaseViewController
14-
15-
@property (nonatomic, strong) ChatModel *chatModel;
14+
//必传
15+
@property (nonatomic, strong) ChatModel *config;
1616

1717
@end

0 commit comments

Comments
 (0)