最近比較項(xiàng)目中有一個(gè)視頻上傳的需求恶迈,可以支持多選的涩金。找了一些資料都是比較亂。后來就是通過一個(gè)好友的幫助暇仲。自己總結(jié)了一下步做,實(shí)現(xiàn)了這個(gè)需求,但是還是有點(diǎn)小問題有待解決奈附。這里就給大家分享一下全度。
1,首先要獲取到本地的視頻
2,開始選取視頻斥滤,可以定一個(gè)Model這樣用起來比較方便
偽代碼如下:
#import@interface GCMAssetModel : NSObject
@property (nonatomic,strong) UIImage *thumbnail;//縮略圖
@property (nonatomic,copy) NSURL *imageURL;//原圖url
@property (nonatomic,assign) BOOL isSelected;//是否被選中
@property (nonatomic,assign) BOOL isImage;//是否是圖片
- (void)originalImage:(void (^)(UIImage *image))returnImage;//獲取原圖
@end
3将鸵,選取視頻展示到Collectionview上面,我項(xiàng)目中做的是類似于圖片的這種格式
4,步驟如下:點(diǎn)擊加好按鈕進(jìn)行選擇挑胸,之后返回選擇的視頻的數(shù)組一喘,下邊有一步重要的壓縮轉(zhuǎn)碼操作接下來會(huì)貼出方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{NSLog(@"您點(diǎn)擊了添加");
if (indexPath.row == _ImageArr.count) {
GCMImagePickerController *picker = [[GCMImagePickerController alloc] init];
//返回選中的原圖
[picker setDidFinishSelectImageModels:^(NSMutableArray *models) {
// NSLog(@"原圖%@",models);
for (GCMAssetModel *model in models) {
FileModel *fileModell = [[FileModel alloc] init];
fileModell.filename = [NSString stringWithFormat:@"%ld.mp4",RandomNum];
fileModell.fileAssetURL = model.imageURL;
//壓縮轉(zhuǎn)碼操作
[self convertVideoToData:fileModell];
[fileModelArray addObject:fileModell];
[_ImageArr addObject:model.thumbnail];
NSLog(@"111%@",_ImageArr);
[_collectionview reloadData];}
}];
[self presentViewController:picker animated:YES completion:nil];}
}
5,壓縮轉(zhuǎn)碼操作
//壓縮轉(zhuǎn)碼
- (void)convertVideoToData:(FileModel *)model
{//保存至沙盒路徑
NSString *pathDocuments =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(@"%@",pathDocuments);
NSString *videoPath = [NSString stringWithFormat:@"%@/Video",pathDocuments];
model.sandBoxPath = [videoPath stringByAppendingPathComponent:model.filename];
//轉(zhuǎn)碼配置
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:model.fileAssetURL options:nil];
AVAssetExportSession *exportSession= [[AVAssetExportSession alloc]
initWithAsset:asset presetName:AVAssetExportPresetLowQuality];
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputURL = [NSURL fileURLWithPath:model.sandBoxPath];
exportSession.outputFileType = AVFileTypeMPEG4;
NSArray *compatiblePresets = [AVAssetExportSession
exportPresetsCompatibleWithAsset:asset];
NSLog(@"%@",compatiblePresets);
[exportSession exportAsynchronouslyWithCompletionHandler:^{
int exportStatus = exportSession.status;
NSLog(@"%d",exportStatus);
switch (exportStatus)
{
case AVAssetExportSessionStatusFailed:{
_hud1.labelText = @"不能創(chuàng)建文件";
[_hud1 hide:YES afterDelay:0.5];
break;}
case AVAssetExportSessionStatusCompleted:{
NSLog(@"視頻轉(zhuǎn)碼成功");
//視頻的NSData數(shù)據(jù)在模型fileData屬性里
NSError *error;
NSData * data = [NSData dataWithContentsOfFile:model.sandBoxPath options:
(NSDataReadingMappedIfSafe) error:&error];
model.fileData = data;
[_VoideData addObject:data];
NSLog(@"%@",model.sandBoxPath);
//判斷多選的情況下嗜暴,什么時(shí)候視頻全部轉(zhuǎn)碼成功
if (_VoideData.count==fileModelArray.count) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"全部結(jié)束");
[_hud1 hide:YES];});
}
break;}
}
}];
}
6凸克,將視頻文件保存至本地 此方法要先調(diào)用,因?yàn)槭紫纫_辟儲(chǔ)存空間闷沥,寫在ViewDidLoad里面調(diào)用
- (void)creatSandBoxFilePathIfNoExist{
//沙盒路徑
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSLog(@"databse--->%@",documentDirectory);
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *pathDocuments =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//創(chuàng)建目錄
_createPath = [NSString stringWithFormat:@"%@/Video",pathDocuments];
// 判斷文件夾是否存在萎战,如果不存在,則創(chuàng)建
if (![[NSFileManager defaultManager] fileExistsAtPath:_createPath]) {
[fileManager createDirectoryAtPath:_createPath withIntermediateDirectories:YES attributes:nil error:nil];} else {
NSLog(@"FileImage is exists.");}}
7舆逃,上傳的話每個(gè)人都有每個(gè)人的方法蚂维,可以用AFN進(jìn)行上傳,注意上傳的時(shí)候是上傳的NsData數(shù)據(jù)路狮。這里我就不貼代碼了虫啥。