2
閱讀了幾個教程like this並查看導出視頻的其他代碼後,我們仍然無法解決問題。視頻不總是導出到相機卷:NSFileManager的removeItemAtPath非阻塞?
有時會將新視頻導出到相機膠捲,有時候不會。我們甚至無法一直重現這個問題。
我們可以想象的唯一問題是如果NSFileManager.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")
}
})
}
嗨,馬克任何機會,你可以用這個問題的幫助? http://stackoverflow.com/questions/34704032/swift-video-records-at-one-size-but-renders-at-wrong-size。謝謝! – Crashalot
我回復了@Crashalot http://stackoverflow.com/a/34794554/51700 –
非常感謝Mark! – Crashalot