gpt4 book ai didi

ios - assetExport完成后检索路径

转载 作者:行者123 更新时间:2023-11-30 13:54:07 25 4
gpt4 key购买 nike

我正在函数中导出视频,完成后路径将添加到变量中。在另一种方法中,我想调用此方法,然后在完成时保存它。我尝试过使用后台队列,但它一直说变量为零?我怎样才能实现这个目标?

创建视频

func createVideo() {

// create new file to receive data
let docsDir: AnyObject = documentsPath
let movieFilePath = docsDir.stringByAppendingPathComponent("result.mov")
let movieDestinationUrl = NSURL(fileURLWithPath: movieFilePath)
_ = try? NSFileManager().removeItemAtURL(movieDestinationUrl)

// use AVAssetExportSession to export video
let assetExport = AVAssetExportSession(asset: composition, presetName:AVAssetExportPresetHighestQuality)
assetExport!.outputFileType = AVFileTypeQuickTimeMovie
assetExport!.outputURL = movieDestinationUrl
assetExport?.videoComposition = layercomposition

assetExport!.exportAsynchronouslyWithCompletionHandler({
switch assetExport!.status{
case AVAssetExportSessionStatus.Failed:
print("failed \(assetExport!.error)")
case AVAssetExportSessionStatus.Cancelled:
print("cancelled \(assetExport!.error)")
default:
print("Movie complete")


// save to photoalbum
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in


self.movieUrl = movieFilePath
})


}

})

}

保存视频

func saveVideo() {

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {
self.createVideo()

dispatch_async(dispatch_get_main_queue(), {
UISaveVideoAtPathToSavedPhotosAlbum(self.movieUrl!, self, "image:didFinishSavingWithError:contextInfo:", nil)

})
})


}

最佳答案

看起来,在我们在 block 内分配它的值之前,movieFilepath 变量被释放。您应该在 dispatch_get_mainQueue() 中使用 dispatch_queue

func createVideo() {

// create new file to receive data
let docsDir: AnyObject = documentsPath
let movieFilePath = docsDir.stringByAppendingPathComponent("result.mov")
let movieDestinationUrl = NSURL(fileURLWithPath: movieFilePath)
_ = try? NSFileManager().removeItemAtURL(movieDestinationUrl)

// use AVAssetExportSession to export video
let assetExport = AVAssetExportSession(asset: composition, presetName:AVAssetExportPresetHighestQuality)
assetExport!.outputFileType = AVFileTypeQuickTimeMovie
assetExport!.outputURL = movieDestinationUrl
assetExport?.videoComposition = layercomposition

assetExport!.exportAsynchronouslyWithCompletionHandler({
switch assetExport!.status{
case AVAssetExportSessionStatus.Failed:
print("failed \(assetExport!.error)")
case AVAssetExportSessionStatus.Cancelled:
print("cancelled \(assetExport!.error)")
default:
print("Movie complete")

dispatch_async(dispatch_get_main_queue(),{

self.movieUrl = movieFilePath
})

}
})
}

这是因为,我们不知道 NSOperationQueue 何时执行该操作。

关于ios - assetExport完成后检索路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33878020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com