您好,登錄后才能下訂單哦!
小編給大家分享一下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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。