【发布时间】:2018-05-09 02:32:35
【问题描述】:
我正在尝试使用 UIImagePickerController 保存使用相机录制的视频。我尝试了两种不同的方法,但都根本不起作用。
第一次尝试:
if let pathURL = info["UIImagePickerControllerMediaURL"] as! URL?, mediaType == kUTTypeMovie {
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: pathURL)
}, completionHandler: { (isSuccessfull, error) in
if error != nil {
// ERROR
// error.localizedDescription value below
// "The operation couldn’t be completed. (Cocoa error -1.)"
} else if isSuccessfull {
// SUCCESS
}
})
}
在那次尝试中,我总是收到错误"The operation couldn’t be completed. (Cocoa error -1.)"
第二次尝试:
if let filePath = info["UIImagePickerControllerMediaURL"] as! NSURL,
let stringPath = filePath.path {
UISaveVideoAtPathToSavedPhotosAlbum(stringPath, self, #selector(Controller.videoSaved(videoPath:didFinishSavingWithError:contextInfo:)), nil)
}
这也不起作用...回调,错误为零,但视频未保存到相机胶卷中。此外,当我使用UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(stringPath) 时,此方法也会一直返回我false。我不明白为什么录制的视频无法保存。
我真的不知道我做错了什么。我错过了什么? plist 中的配置?
info["UIImagePickerControllerMediaURL"] 的值如下所示:
file:///private/var/mobile/Containers/Data/Application/2B9BE04A-17B3-49CE-B4BA-C45F183E9A64/tmp/837294779101__C4825AFE-8140-420F-ACD0-64623C7A4753.MOV
对于用相机拍摄的照片,PHPhotoLibrary 方法工作得很好......只有视频无法保存。
【问题讨论】:
-
查看这个有问题的答案stackoverflow.com/questions/9991332/…
-
谢谢 Jitendra。我尝试将相同的代码直接放在委托方法中并且它有效。我最初是在 API 调用后进行此保存操作的。我的假设是该路径中的文件可能在几秒钟后不可用...
-
您可以使用此密钥
UIImagePickerControllerPHAsset从信息中获取资产。 -
@InfinityJames,当我录制视频时,字典只包含这些键:
UIImagePickerControllerMediaURL和UIImagePickerControllerMediaType。
标签: ios swift phphotolibrary