视频是否不总是导出到相机胶卷:NSFileManager的removeItemAtPath是否无阻塞? [英] Video not always exported to Camera Roll: NSFileManager's removeItemAtPath non-blocking?
问题描述
在阅读了一些教程像这样并查看了导出视频的其他代码后,我们仍然可以无法解决问题.
After reading several tutorials like this and looking at other code exporting videos, we still can't resolve an issue.
有时,新视频会导出到相机胶卷"中,有时却不会.我们甚至无法始终如一地重现该问题.
Sometimes a new video gets exported to the Camera Roll, and sometimes it doesn't. We can't even reproduce the problem consistently.
我们可以想象的唯一问题是 NSFileManager.defaultManager().removeItemAtPath
不是阻塞调用,但是没有文档表明它是异步的,因此我们认为情况并非如此.
The only issue we can imagine is if NSFileManager.defaultManager().removeItemAtPath
is not a blocking call, but no documentation suggests it's asynchronous, so we assume it's not the case.
每次,都会调用 writeVideoAtPathToSavedPhotosAlbum
闭包中的已保存视频" println
,这表明该视频已成功写入相机胶卷",但我们看不到那里的视频.
Each time, the "Saved video" println
inside the writeVideoAtPathToSavedPhotosAlbum
closure gets called, suggesting the video was successfully written to the Camera Roll, but we don't see the video there.
有关如何解决问题的建议?
Suggestions on how to troubleshoot?
代码:
// -- 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")
}
})
}
推荐答案
一些建议可能有助于解决您的问题:
-
按照此处
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:videoURL])
{
[library writeVideoAtPathToSavedPhotosAlbum:videoURL
completionBlock:^(NSURL *assetURL, NSError *error){}
];
}
考虑使用PHPhotoLibrary代替导出视频
consider using the PHPhotoLibrary instead for exporting video
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
})
这篇关于视频是否不总是导出到相机胶卷:NSFileManager的removeItemAtPath是否无阻塞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!