Skip to content

Commit 275fc9c

Browse files
author
mengyaoyao
committed
视频消息缓存处理
1 parent aec285e commit 275fc9c

File tree

19 files changed

+148
-111
lines changed

19 files changed

+148
-111
lines changed

CocoaAsyncSocket_TCP.xcodeproj/project.pbxproj

Lines changed: 45 additions & 41 deletions
Large diffs are not rendered by default.

CocoaAsyncSocket_TCP/ChatHandler/ChatUtil.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
@class ChatModel,ChatAlbumModel;
1212

13+
typedef void(^videoModelCallback)(ChatModel *videoModel);
1314
@interface ChatUtil : NSObject
1415

1516
//消息高度计算
@@ -22,6 +23,6 @@
2223
//初始化图片消息模型
2324
+ (NSArray<ChatModel *> *)initPicMessage:(NSArray<ChatAlbumModel *> *)pics config:(ChatModel *)config;
2425
//初始化视频消息模型
25-
+ (ChatModel *)initVideoMessage:(ChatAlbumModel *)video config:(ChatModel *)config;
26+
+ (void)initVideoMessage:(ChatAlbumModel *)video config:(ChatModel *)config videoCallback:(videoModelCallback)callback;
2627

2728
@end

CocoaAsyncSocket_TCP/ChatHandler/ChatUtil.m

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "ChatModel.h"
1111
#import "ChatAlbumModel.h"
1212
#import "MYCoreTextLabel.h"
13+
#import <Photos/Photos.h>
1314

1415
@implementation ChatUtil
1516

@@ -242,9 +243,72 @@ + (ChatModel *)initAudioMessage:(ChatAlbumModel *)audio config:(ChatModel *)conf
242243
return tmpArray;
243244
}
244245
//初始化视频消息模型
245-
+ (ChatModel *)initVideoMessage:(ChatAlbumModel *)video config:(ChatModel *)config
246+
+ (void)initVideoMessage:(ChatAlbumModel *)video config:(ChatModel *)config videoCallback:(videoModelCallback)callback
246247
{
247-
return nil;
248+
PHAsset *asset = video.videoAsset;
249+
NSString *basePath = nil;
250+
NSArray *assetResources = [PHAssetResource assetResourcesForAsset:asset];
251+
PHAssetResource *resource;
252+
253+
for (PHAssetResource *assetRes in assetResources) {
254+
if (assetRes.type == PHAssetResourceTypePairedVideo ||
255+
assetRes.type == PHAssetResourceTypeVideo) {
256+
resource = assetRes;
257+
}
258+
}
259+
if (hashEqual(config.chatType, @"userChat")) {
260+
basePath = [ChatCache_Path stringByAppendingPathComponent:config.toUserID];
261+
}else{
262+
basePath = [ChatCache_Path stringByAppendingPathComponent:config.groupID];
263+
}
264+
265+
NSFileManager *manager = [NSFileManager defaultManager];
266+
BOOL exist = [manager fileExistsAtPath:basePath];
267+
if (!exist) {
268+
[manager createDirectoryAtPath:basePath withIntermediateDirectories:YES attributes:nil error:NULL];
269+
}
270+
//具体路径
271+
NSString *detailPath = [basePath stringByAppendingString:video.name];
272+
273+
//创建视频消息模型
274+
ChatModel *videoModel = [self creatMessageModel:config];
275+
videoModel.contenType = Content_Video;
276+
videoModel.content.fileName = video.name;
277+
278+
//异步存储
279+
dispatch_async(dispatch_get_global_queue(0, 0), ^{
280+
281+
if (asset.mediaType == PHAssetMediaTypeVideo || asset.mediaSubtypes == PHAssetMediaSubtypePhotoLive) {
282+
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
283+
options.version = PHImageRequestOptionsVersionCurrent;
284+
options.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat; //此处可调节质量
285+
[[NSFileManager defaultManager] removeItemAtPath:detailPath error:nil];
286+
[[PHAssetResourceManager defaultManager] writeDataForAssetResource:resource
287+
toFile:[NSURL fileURLWithPath:detailPath]
288+
options:nil
289+
completionHandler:^(NSError * _Nullable error) {
290+
if (error) {
291+
292+
dispatch_async(dispatch_get_main_queue(), ^{
293+
callback(nil);
294+
});
295+
296+
} else {
297+
long long int size = [[NSData dataWithContentsOfFile:detailPath]length];
298+
dispatch_async(dispatch_get_main_queue(), ^{
299+
videoModel.content.fileSize = [@(size)stringValue];
300+
callback(videoModel);
301+
NSLog(@"------------相册视频回调----------");
302+
});
303+
}
304+
}];
305+
} else {
306+
dispatch_async(dispatch_get_main_queue(), ^{
307+
308+
callback(nil);
309+
});
310+
}
311+
});
248312
}
249313

250314

CocoaAsyncSocket_TCP/ChatModel/ChatAlbumModel.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
#import <Foundation/Foundation.h>
10+
#import <Photos/Photos.h>
1011

1112
@interface ChatAlbumModel : NSObject
1213

@@ -25,7 +26,7 @@
2526
//图片尺寸
2627
@property (nonatomic, assign) CGSize picSize;
2728
//视频缓存地址
28-
@property (nonatomic, copy) NSString *videoCachePath;
29+
@property (nonatomic, strong) PHAsset *videoAsset;
2930
//视频缩略图
3031
@property (nonatomic, strong) UIImage *videoCoverImg;
3132
//视频 , 语音时长

CocoaAsyncSocket_TCP/Comon/Category/UIImage+photoPicker.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
typedef void(^photoPickerImagesCallback)(NSArray<ChatAlbumModel *> *images);
1414

1515
//返回视频存储的位置
16-
typedef void(^videoPathCallback)(ChatAlbumModel *videoModel);
16+
typedef void(^videoBaseInfoCallback)(ChatAlbumModel *videoModel);
1717

1818

1919
@interface UIImage (photoPicker)
@@ -28,10 +28,7 @@ typedef void(^videoPathCallback)(ChatAlbumModel *videoModel);
2828

2929
/**
3030
获取选中的视频
31-
32-
@param videoPathCallback <#videoPathFileNameCallback description#>
33-
@param target <#target description#>
3431
*/
35-
+ (void)openPhotoPickerGetVideo:(videoPathCallback)videoPathCallback target:(UIViewController *)target cacheDirectory:(NSString *)basePath;
32+
+ (void)openPhotoPickerGetVideo:(videoBaseInfoCallback)callback target:(UIViewController *)target cacheDirectory:(NSString *)basePath;
3633

3734
@end

CocoaAsyncSocket_TCP/Comon/Category/UIImage+photoPicker.m

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,16 @@ @implementation UIImage (photoPicker)
2020
下载获取视频
2121
2222
*/
23-
+ (void)openPhotoPickerGetVideo:(videoPathCallback)videoPathCallback target:(UIViewController *)target cacheDirectory:(NSString *)basePath
23+
+ (void)openPhotoPickerGetVideo:(videoBaseInfoCallback)callback target:(UIViewController *)target
2424
{
25-
26-
if (![[NSFileManager defaultManager]fileExistsAtPath:basePath]) {
27-
28-
[[NSFileManager defaultManager]createDirectoryAtPath:basePath withIntermediateDirectories:YES attributes:nil error:nil];
29-
}
30-
3125
//每次只能选取一个视频
3226
TZImagePickerController *picker = [self initPickerWithtaget:target maxCount:1];
3327
picker.allowPickingImage = NO;
3428
picker.allowPickingVideo = YES;
3529
picker.didFinishPickingVideoHandle = ^(UIImage *coverImage,id asset){
3630

3731
//缓存视频到本地
38-
[self getVideoPathFromPHAsset:asset cachePath:basePath complete:videoPathCallback cover:coverImage];
39-
32+
[self getVideoPathFromPHAsset:asset complete:callback cover:coverImage];
4033
};
4134
}
4235

@@ -53,11 +46,9 @@ + (void)openPhotoPickerGetVideo:(videoPathCallback)videoPathCallback target:(UIV
5346
缓存视频到本地
5447
5548
@param asset <#asset description#>
56-
@param basePath <#basePath description#>
57-
@param videPathCallback <#videPathCallback description#>
5849
@param cover <#coverCallbackNow description#>
5950
*/
60-
+ (void)getVideoPathFromPHAsset:(PHAsset *)asset cachePath:(NSString *)basePath complete:(videoPathCallback)videPathCallback cover:(UIImage *)cover {
51+
+ (void)getVideoPathFromPHAsset:(PHAsset *)asset complete:(videoBaseInfoCallback)callback cover:(UIImage *)cover {
6152
NSArray *assetResources = [PHAssetResource assetResourcesForAsset:asset];
6253
PHAssetResource *resource;
6354

@@ -72,52 +63,18 @@ + (void)getVideoPathFromPHAsset:(PHAsset *)asset cachePath:(NSString *)basePath
7263
//命名规范可以自行定义 , 但是要保证不要重复
7364
fileName = [NSString stringWithFormat:@"chatVideo_%@%@",getCurrentTime(),resource.originalFilename];
7465
}
75-
//视频存储路径
76-
NSString *PATH_MOVIE_FILE = [basePath stringByAppendingPathComponent:fileName];
7766
//创建视频模型
7867
ChatAlbumModel *videoModel = [[ChatAlbumModel alloc]init];
7968
//缩略图
8069
videoModel.videoCoverImg = cover;
81-
//缓存路径
82-
videoModel.videoCachePath = PATH_MOVIE_FILE;
8370
//视频时长
8471
videoModel.duration = [@(asset.duration)stringValue];
85-
86-
//异步存储
87-
dispatch_async(dispatch_get_global_queue(0, 0), ^{
88-
89-
if (asset.mediaType == PHAssetMediaTypeVideo || asset.mediaSubtypes == PHAssetMediaSubtypePhotoLive) {
90-
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
91-
options.version = PHImageRequestOptionsVersionCurrent;
92-
options.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat; //此处可调节质量
93-
94-
[[NSFileManager defaultManager] removeItemAtPath:PATH_MOVIE_FILE error:nil];
95-
[[PHAssetResourceManager defaultManager] writeDataForAssetResource:resource
96-
toFile:[NSURL fileURLWithPath:PATH_MOVIE_FILE]
97-
options:nil
98-
completionHandler:^(NSError * _Nullable error) {
99-
if (error) {
100-
101-
dispatch_async(dispatch_get_main_queue(), ^{
102-
videPathCallback(nil);
103-
});
104-
105-
} else {
106-
long long int size = [[NSData dataWithContentsOfFile:PATH_MOVIE_FILE]length];
107-
dispatch_async(dispatch_get_main_queue(), ^{
108-
videoModel.size = [@(size)stringValue];
109-
videPathCallback(videoModel);
110-
NSLog(@"------------相册视频回调----------");
111-
});
112-
}
113-
}];
114-
} else {
115-
dispatch_async(dispatch_get_main_queue(), ^{
116-
117-
videPathCallback(nil);
118-
});
119-
}
120-
});
72+
//视频名称
73+
videoModel.name = fileName;
74+
//回调含有基本信息的视频模型
75+
if (callback) {
76+
callback(videoModel);
77+
}
12178
}
12279

12380

CocoaAsyncSocket_TCP/Controller/ChatViewController.m

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
224224
}else if (hashEqual(chatModel.contenType, Content_Video)){
225225

226226
ChatVideoCell *videoCell = [tableView dequeueReusableCellWithIdentifier:@"ChatVideoCell"];
227-
227+
videoCell.videoModel = chatModel;
228228
return videoCell;
229229

230230
//文件消息
@@ -310,7 +310,14 @@ - (void)sendPictureMessage:(NSArray<ChatAlbumModel *> *)picModels
310310
#pragma mark - 发送视频消息
311311
- (void)sendVideoMessage:(ChatAlbumModel *)videoModel
312312
{
313-
313+
//视频消息基本信息配置
314+
[ChatUtil initVideoMessage:videoModel config:_config videoCallback:^(ChatModel *videoModel) {
315+
316+
[self.talkMessages addObject:videoModel];
317+
[self.chatTableView reloadData];
318+
[self scrollToBottom];
319+
[[ChatHandler shareInstance]sendVideoMessage:videoModel];
320+
}];
314321
}
315322

316323
#pragma mark - 滚动,点击等相关处理

CocoaAsyncSocket_TCP/View/ChatCells/ChatVideoCell.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
#import <UIKit/UIKit.h>
1010

11+
@class ChatModel;
12+
1113
@interface ChatVideoCell : UITableViewCell
1214

15+
@property (nonatomic, strong) ChatModel *videoModel;
16+
1317
@end

CocoaAsyncSocket_TCP/View/ChatCells/ChatVideoCell.m

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@
1010

1111
@implementation ChatVideoCell
1212

13-
- (void)awakeFromNib {
14-
[super awakeFromNib];
15-
// Initialization code
13+
14+
- (void)setVideoModel:(ChatModel *)videoModel
15+
{
16+
_videoModel = videoModel;
17+
18+
19+
20+
21+
1622
}
1723

18-
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
19-
[super setSelected:selected animated:animated];
2024

21-
// Configure the view for the selected state
22-
}
2325

2426
@end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy