视频并不总是导出到相机胶卷:NSFileManager 的 removeItemAtPath 非阻塞?
Posted
技术标签:
【中文标题】视频并不总是导出到相机胶卷:NSFileManager 的 removeItemAtPath 非阻塞?【英文标题】:Video not always exported to Camera Roll: NSFileManager's removeItemAtPath non-blocking? 【发布时间】:2015-06-25 18:34:04 【问题描述】:看了几篇教程like this 并查看了其他代码导出视频,我们仍然无法解决问题。
有时新视频会导出到相机胶卷,有时则不会。我们甚至无法始终如一地重现该问题。
我们可以想象的唯一问题是NSFileManager.defaultManager().removeItemAtPath
是否不是阻塞调用,但没有文档表明它是异步的,因此我们假设情况并非如此。
每次,writeVideoAtPathToSavedPhotosAlbum
闭包内的“已保存视频”println
都会被调用,这表明视频已成功写入相机胶卷,但我们看不到该视频。
关于如何排除故障的建议?
代码:
// -- 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")
)
【问题讨论】:
【参考方案1】:一些可能有助于解决您的问题的建议:
检查视频是否有效/与库兼容,如here
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:videoURL])
[library writeVideoAtPathToSavedPhotosAlbum:videoURL
completionBlock:^(NSURL *assetURL, NSError *error)
];
确保传回的资产 url 不为引用 here 的 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]);
考虑使用 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
)
【讨论】:
您好,请问您有没有机会为这个问题提供帮助? ***.com/questions/34704032/…。谢谢! 非常感谢马克!以上是关于视频并不总是导出到相机胶卷:NSFileManager 的 removeItemAtPath 非阻塞?的主要内容,如果未能解决你的问题,请参考以下文章