效果图(由于上传gif图大小受限 录制时长比较短):
-
上一篇已实现多张图片的发送
- 接下来实现传输进度的回调
- 实现单个及多个视频的发送,视频的传输跟图片传输策略是一样的 只是获取视频流比图片多几部操作而已
一.传输进度回调 新建 HJProgressHub 类,在ChatMessageCell 根据状态显示隐藏
#import "HJProgressHub.h"
@interface HJProgressHub()
/// 菊花
@property (nonatomic, strong) UIActivityIndicatorView *indicatorView;
/// 灰色的板子
@property (nonatomic, strong) UIView *grayView;
/// 进度label
@property (nonatomic, strong) UILabel *progressLabel;
@end
SocketManager 代理回调中 取出当前cell赋值操作
#pragma mark - SocketManagerDelegate
- (void)socketManager:(SocketManager *)manager itemUpingrefresh:(ChatMessageModel *)upingItem{
// 刷新当前进度
ChatMessageCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:upingItem.locationIndex inSection:0]];
cell.progressHub.progress = 1.0 * upingItem.upSize / upingItem.fileSize;
}
二.视频资源的获取 同样是由 ESKeyboardToolView 触发
- (void)sendImageOrVideo{
WS(weakSelf);
[[PickerImageVideoTool sharePickerImageVideoTool] showImagePickerWithMaxCount:9 completion:^(NSArray<UIImage *> *photos, NSArray *assets) {
NSInteger count = assets.count;
id objc = nil;
for (NSInteger i = 0; i < count; i++) {
objc = assets[i];
if (![objc isKindOfClass:[PHAsset class]]) {
continue;
}
PHAsset *asset = (PHAsset *)objc;
ChatMessageModel *messageM = [ChatMessageModel new];
messageM.isFormMe = YES;
messageM.userName = [UIDevice currentDevice].name;
messageM.asset = asset;
messageM.fileName = [ZPPublicMethod getAssetsName:asset only:YES];
if (asset.mediaType == PHAssetMediaTypeImage){
messageM.temImage = photos[i];
[weakSelf pickImageHandle:messageM];
}else if (asset.mediaType == PHAssetMediaTypeVideo) {
messageM.chatMessageType = ChatMessageVideo;
[weakSelf pickVideoHandle:messageM];
}else if (asset.mediaType == PHAssetMediaTypeAudio){
messageM.chatMessageType = ChatMessageAudio;
}
}
}];
}
// 获取图片的操作
- (void)pickImageHandle:(ChatMessageModel*)messageM{
messageM.chatMessageType = ChatMessageImage;
messageM.fileSize = UIImagePNGRepresentation(messageM.temImage).length;
SDImageCache *cache = [SDImageCache sharedImageCache];
[cache storeImage:messageM.temImage forKey:messageM.fileName toDisk:YES completion:^{
messageM.showImageUrl = [NSURL fileURLWithPath:[cache defaultCachePathForKey:messageM.fileName]];
NSLog(@"imgMessageUrl = %@",messageM.mediaMessageUrl);
}];
[self sendMessageWithItem:messageM];
}
获取视频的操作
- (void)pickVideoHandle:(ChatMessageModel *)messageM{
// 视频getPhotoWithAsset
[ZPPublicMethod getfilePath:messageM.asset Complete:^(NSURL *fileUrl) {
messageM.fileSize = [ZPPublicMethod getFileSize:[[fileUrl absoluteString] substringFromIndex:8]];
[ZPPublicMethod getThumbnail:messageM.asset size:CGSizeMake(375, 667) result:^(UIImage *thumImage) {
if (messageM.temImage == nil) {
messageM.temImage = thumImage;
SDImageCache *cache = [SDImageCache sharedImageCache];
NSString *keyStr = [messageM.fileName stringByAppendingString:@".JPG"];
[cache storeImage:messageM.temImage forKey:keyStr toDisk:YES completion:^{
messageM.showImageUrl = [NSURL fileURLWithPath:[cache defaultCachePathForKey:keyStr]];
}];
[self sendMessageWithItem:messageM];
}
}];
}];
}
通过PHAsset 获取资源URL地址方法实现
/// 通 PHAsset 获取文件url
+ (void)getfilePath:(PHAsset *)asset Complete:(void(^)(NSURL *fileUrl))result{
if (asset.mediaType == PHAssetMediaTypeVideo) {
PHVideoRequestOptions *videoOption = [[PHVideoRequestOptions alloc] init];
videoOption.version = PHVideoRequestOptionsVersionOriginal;
[[PHImageManager defaultManager] requestAVAssetForVideo:asset options:videoOption resultHandler:^(AVAsset * _Nullable videoAsset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
AVURLAsset *urlAsset = (AVURLAsset *)videoAsset;
result(urlAsset.URL);
}];
}else if (asset.mediaType == PHAssetMediaTypeImage){
// 获取图片资源路径
PHContentEditingInputRequestOptions *reqOption = [[PHContentEditingInputRequestOptions alloc] init];
reqOption.canHandleAdjustmentData = ^(PHAdjustmentData * _Nonnull adjustmentData) {
return YES;
};
[asset requestContentEditingInputWithOptions:reqOption completionHandler:^(PHContentEditingInput * _Nullable contentEditingInput, NSDictionary * _Nonnull info) {
result(contentEditingInput.fullSizeImageURL);
}];
}
}
通过PHAsset 获取资源缩略图
+ (void)getThumbnail:(PHAsset *)asset size:(CGSize)size result:(void(^)(UIImage *thumImage))result{
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.version = PHImageRequestOptionsVersionCurrent;
[[PHImageManager defaultManager] requestImageForAsset:asset
targetSize:size
contentMode:PHImageContentModeAspectFill
options:nil
resultHandler:^(UIImage *image, NSDictionary *info) {
dispatch_async(dispatch_get_main_queue(), ^{
if (result) {
result(image);
}
});
}];
}
SocketManager 中只需把昨天预留处理视频的地方写下就OK
// 图片或者视频文件传输
- (void)imageOrVideoFileSend:(ChatMessageModel *)sendItem{
if (sendItem.chatMessageType == ChatMessageImage) {
NSData *sendData = UIImagePNGRepresentation(sendItem.temImage);
[self writeMediaMessageWithData:sendData];
}else if (sendItem.chatMessageType == ChatMessageVideo){
PHAsset *asset = (PHAsset *)sendItem.asset;
[ZPPublicMethod getfilePath:asset Complete:^(NSURL *fileUrl) {
dispatch_sync(dispatch_get_main_queue(), ^{
sendItem.mediaMessageUrl = fileUrl;
NSData *sendData = [NSData dataWithContentsOfURL:sendItem.mediaMessageUrl options:NSDataReadingMappedIfSafe error:nil];
[self writeMediaMessageWithData:sendData];
});
}];
}
}