iOS 13以后调用原生视频编辑导出失败

iOS视频编辑系统自带控制器UIVideoEditorController目前发现编辑视频后无法导出
在这里插入图片描述

iOS13之前编辑完回调用UIVideoEditorController的三个代理方法
videoEditorController:didSaveEditedVideoToPath:

videoEditorController: didFailWithError:

videoEditorControllerDidCancel:
编辑完成后 videoEditorController:didSaveEditedVideoToPath:
代理方法会给出编辑后的地址,可是iOS13不知道苹果又有啥骚操作导致不能正常导出地址而是直接走第三个videoEditorControllerDidCancel:
取消的方法,怎么解决呢?通过我的实践使用#import <AVFoundation/AVFoundation.h>
框架进行视频编辑可以适配一下iOS13系统编辑视频的问题
核心代码
#pragma mark 视频裁剪

  • (void)notifyDelegateOfDidChange{
    self.tempVideoPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@“tmpMov.mov”];

    [self deleteTempFile];

    AVAsset *asset = [AVAsset assetWithURL:self.videoUrl];
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
    initWithAsset:asset presetName:AVAssetExportPresetPassthrough];

    NSURL *furl = [NSURL fileURLWithPath:self.tempVideoPath];
    exportSession.outputURL = furl;
    exportSession.outputFileType = AVFileTypeQuickTimeMovie;

    CMTime start = CMTimeMakeWithSeconds(self.startTime, self.player.currentTime.timescale);
    CMTime duration = CMTimeMakeWithSeconds(self.endTime - self.startTime, self.player.currentTime.timescale);;
    CMTimeRange range = CMTimeRangeMake(start, duration);
    exportSession.timeRange = range;

    [exportSession exportAsynchronouslyWithCompletionHandler:^{
    switch ([exportSession status]) {
    case AVAssetExportSessionStatusFailed:
    NSLog(@“Export failed: %@”, [[exportSession error] localizedDescription]);
    break;

          case AVAssetExportSessionStatusCancelled:
              NSLog(@"Export canceled");
              break;
              
          case AVAssetExportSessionStatusCompleted:{
              NSLog(@"Export completed");
              __weak typeof(self) weakSelf = self;
              dispatch_async(dispatch_get_main_queue(), ^{
                  UISaveVideoAtPathToSavedPhotosAlbum([furl relativePath], self,@selector(video:didFinishSavingWithError:contextInfo:), nil);
                  NSLog(@"编辑后的视频路径: %@",weakSelf.tempVideoPath);
                  
                  weakSelf.isEdited = YES;
                  [weakSelf invalidatePlayer];
                  [weakSelf initPlayerWithVideoUrl:furl];
                  bottomView.hidden = YES;
              });
          }
              break;
              
          default:
              NSLog(@"Export other");
    
              break;
      }
    

    }];
    }

  • (void)video:(NSString*)videoPath didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo {
    if (error) {
    NSLog(@“保存到相册失败”);
    }
    else {
    NSLog(@“保存到相册成功”);
    }
    }

  • (void)deleteTempFile{
    NSURL *url = [NSURL fileURLWithPath:self.tempVideoPath];
    NSFileManager *fm = [NSFileManager defaultManager];
    BOOL exist = [fm fileExistsAtPath:url.path];
    NSError *err;
    if (exist) {
    [fm removeItemAtURL:url error:&err];
    NSLog(@“file deleted”);
    if (err) {
    NSLog(@“file remove error, %@”, err.localizedDescription );
    }
    }
    else {
    NSLog(@“no file by that name”);
    }
    }

使用比较简单主要就是利用AVFoundation框架完成简单的视频编辑以及保存
demo参考了VideoEditDemo-master git地址https://github.com/MisterZhiWei/VideoEditDemo


版权声明:本文为shizhijie000原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。