Progress bar for AVAssetExportSession

后端 未结 4 603
遇见更好的自我
遇见更好的自我 2021-02-02 11:57

I\'ve got an app that exports an AVMutableComposition into a .mov file, and I\'d like for the user to see the status of the export with a progress bar

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-02 12:47

    Use below lines of code.

    AVAssetExportSession *session = [AVAssetExportSession exportSessionWithAsset:composition presetName:AVAssetExportPresetMediumQuality];
    self.exportSession = session;
    
    // 出力先(テンポラリファイル)の設定。
    NSString *filePath = NSTemporaryDirectory();
    filePath = [filePath stringByAppendingPathComponent:@"out.mov"];
    [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
    session.outputURL = [NSURL fileURLWithPath:filePath];
    
    // 出力タイプの設定。
    session.outputFileType = AVFileTypeQuickTimeMovie;
    
    // 非同期エクスポートの開始。
    [session exportAsynchronouslyWithCompletionHandler:^{
        if (session.status == AVAssetExportSessionStatusCompleted) {
            // フォトアルバムへの書き込み。
            ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
            [library writeVideoAtPathToSavedPhotosAlbum:session.outputURL completionBlock:^(NSURL *assetURL, NSError *error){
                if (error) {
                    self.resultLabel.text = [NSString stringWithFormat:@"アセット書き込み失敗\n%@", error];
                } else {
                    self.resultLabel.text = [NSString stringWithFormat:@"完了\n%@", assetURL];
                }
            }];
            [library autorelease];
        } else if (session.status == AVAssetExportSessionStatusCancelled) {
            self.resultLabel.text = @"エクスポート中断";
        } else {
            self.resultLabel.text = [NSString stringWithFormat:@"エクスポート失敗\n%@", session.error];
        }
    }];
    
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        while (session.status == AVAssetExportSessionStatusExporting) {
            dispatch_sync(dispatch_get_main_queue(), ^{
                self.progressView.progress = session.progress;
            });
        }
    });
    

    Reference Link : https://github.com/keijiro/iOS4BookSampleCode/blob/master/3.3.SimpleExport/Classes/SimpleExportViewController.m

提交回复
热议问题