2

在阅读了几个这样的教程并查看了其他代码导出视频后,我们仍然无法解决问题。

有时新视频会导出到相机胶卷,有时则不会。我们甚至无法始终如一地重现该问题。

我们可以想象的唯一问题是 ifNSFileManager.defaultManager().removeItemAtPath不是阻塞调用,但没有文档表明它是异步的,所以我们假设情况并非如此。

每次,闭包println内的“保存的视频”writeVideoAtPathToSavedPhotosAlbum都会被调用,这表明视频已成功写入相机胶卷,但我们在那里看不到视频。

有关如何排除故障的建议?

代码:

        // -- Get path
        let fileName = "/editedVideo.mp4"
        let allPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
        let docsPath = allPaths[0] as! NSString
        let exportPath = docsPath.stringByAppendingFormat(fileName)
        let exportUrl = NSURL.fileURLWithPath(exportPath as String)!

        println(exportPath)

        // -- Remove old video?
        if NSFileManager.defaultManager().fileExistsAtPath(exportPath as String) {
            println("Deleting existing file\n")
            NSFileManager.defaultManager().removeItemAtPath(exportPath as String, error: nil)
        }

        // -- Create exporter
        let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)
        exporter.videoComposition = mutableComposition
        exporter.outputFileType = AVFileTypeMPEG4
        exporter.outputURL = exportUrl
        exporter.shouldOptimizeForNetworkUse = true

        // -- Export video
        exporter.exportAsynchronouslyWithCompletionHandler({
            self.exportDidFinish(exporter)
        })
    }


    func exportDidFinish(exporter: AVAssetExportSession) {
        println("Finished exporting video!")

        // Write out video to photo album
        let assetLibrary = ALAssetsLibrary()
        assetLibrary.writeVideoAtPathToSavedPhotosAlbum(exporter.outputURL, completionBlock: {(url: NSURL!, error: NSError!) in
            println("Saved video \(exporter.outputURL)")

            if (error != nil) {
                println("Error saving video")
            }
        })
    } 
4

1 回答 1

1

一些可能有助于解决您的问题的建议:

  1. 检查视频是否有效/与库兼容,如此处所做

    if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:videoURL])
    {
        [library writeVideoAtPathToSavedPhotosAlbum:videoURL
                                    completionBlock:^(NSURL *assetURL, NSError *error){}
        ];
    } 
    
  2. 确保传回的资产 url 不是这里引用的 nil

    if(error){
        NSLog(@"Error: Domain = %@, Code = %@", [error domain], [error localizedDescription]);
    } else if(assetURL == nil){
    
        //It's possible for writing to camera roll to fail, without receiving an error message, but assetURL will be nil
        //Happens when disk is (almost) full
        NSLog(@"Error saving to camera roll: no error message, but no url returned");
    
    } else {
        //remove temp file
        NSError *error;
        [[NSFileManager defaultManager] removeItemAtURL:fileURL error:&error];
        if(error){
            NSLog(@"error: %@", [error localizedDescription]);
        }
    
    }
    
  3. 考虑使用 PHPhotoLibrary 来导出视频

    PHPhotoLibrary.sharedPhotoLibrary().performChanges({
        let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(videoURL)
        let assetPlaceholder = assetRequest.placeholderForCreatedAsset
    },
    completionHandler: { success, error in
        // check and handle error
        // do something with your asset local identifier
    })
    
于 2015-07-08T19:54:46.390 回答