我尝试从照片库导出视频,但导出回调从未执行。我定期检查导出进度,进度始终为零。
下面的代码在 99.9% 的情况下都有效,但有时在某些设备上(绝对随机)它会停止工作,只有重新启动 iPhone 才有帮助。
AVAssetExportSession.Status
总是在waiting
state
class FilesInteractor {
static func tempDirectoryPath() -> String {
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
return documentsPath.appendingPathComponent("temp") as String
}
static func createTempDirectory() {
if !FileManager.default.fileExists(atPath: tempDirectoryPath()) {
try? FileManager.default.createDirectory(atPath: tempDirectoryPath(), withIntermediateDirectories: true, attributes: nil)
}
}
static func testVideoURL(name: String, ext: String = "mov") -> URL {
createTempDirectory()
let outputURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("test").appendingPathComponent("\(name).\(ext)", isDirectory: false)
log.debug("Test video URL: \(outputURL)")
return outputURL
}
}
import AVFoundation
let asset = AVAsset()
let outputURL = FilesInteractor.testVideoURL("output")
let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPreset1280x720)
exportSession?.outputFileType = .mov
exportSession?.outputURL = outputURL
try? FileManager.default.removeItem(at: outputURL)
exportSession?.exportAsynchronously(completionHandler: {
print("sometimes never calls")
})
其他视频应用程序也会冻结(Filto、Videoleap):