小编给大家分享一下iOS如何实现视频压缩上传功能,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
代码如下:
- (void)convertVideoWithURL:(NSURL *)url { NSDate *date = [NSDate date]; NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init]; [dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"]; NSString *dateName = [dateformatter stringFromDate:date]; NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject; NSString *pathName = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",dateName]]; NSLog(@"沙盒:%@",pathName); //转码配置 AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil]; AVAssetExportSession *exportSession= [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality]; exportSession.shouldOptimizeForNetworkUse = YES; exportSession.outputURL = [NSURL fileURLWithPath:pathName]; exportSession.outputFileType = AVFileTypeMPEG4; [exportSession exportAsynchronouslyWithCompletionHandler:^{ int exportStatus = exportSession.status; switch (exportStatus) { case AVAssetExportSessionStatusFailed: { // log error to text view NSError *exportError = exportSession.error; NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError); [SVProgressHUD showErrorWithStatus:@"视频压缩失败"]; [SVProgressHUD dismissWithDelay:1.0]; break; } case AVAssetExportSessionStatusCompleted: { self.videoData = [NSData dataWithContentsOfFile:pathName]; [[NetTool shareDL]upLoadVideoWithURL:@"http://192.168.1.102/php/image.php" paremeter:nil data:self.videoData videoName:[NSString stringWithFormat:@"%@.mp4",dateName] progress:^(NSProgress * _Nonnull uploadProgress) { [SVProgressHUD showProgress:1.0*uploadProgress.completedUnitCount/uploadProgress.totalUnitCount status:@"正在上传"]; NSLog(@"正在上传%f%%",(1.0*uploadProgress.completedUnitCount/uploadProgress.totalUnitCount)*100); } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { [SVProgressHUD showSuccessWithStatus:@"上传成功"]; [SVProgressHUD dismissWithDelay:1.0]; } fail:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { [SVProgressHUD showErrorWithStatus:@"上传失败"]; [SVProgressHUD dismissWithDelay:1.0]; }]; } } }]; }
[manager POST:url parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { [formData appendPartWithFileData:videoData name:@"upimage" fileName:videoName mimeType:@"video/mp4"]; } progress:^(NSProgress * _Nonnull uploadProgress) { progress(uploadProgress); } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { success(task,responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { fail(task,error); }];
以上是“iOS如何实现视频压缩上传功能”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。