我正在尝试在我的照片库中保存Google驱动器视频。
我正在使用的代码:
@objc func saveVideosToPhone(videoURL:URL,viewController:UIViewController){
PHPhotoLibrary.requestAuthorization
{ (status) -> Void in
switch (status)
{
case .authorized:
// Permission Granted
print("Write your code here")
let urlData = NSData.init(contentsOf:videoURL)
if(urlData != nil)
{
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0];
let filePath = String.init(format:"\(documentsPath)/%@.mp4",self.randomString(length:5))
urlData?.write(toFile: filePath, atomically: true);
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: NSURL(fileURLWithPath: filePath) as URL)
}) { completed, error in
if completed {
MBProgressHUD.hide(for:viewController.view, animated:false)
UtilityMethods.sharedInstance.showAlert(title:"Save", message:"Video is saved!", viewController:viewController)
print("Video is saved!")
}else{
MBProgressHUD.hide(for:viewController.view, animated:false)
UtilityMethods.sharedInstance.showAlert(title:"Error", message:(error?.localizedDescription)!, viewController:viewController)
print("error from gd",error?.localizedDescription)
}
}}
case .denied:
// Permission Denied
print("User denied")
default:
print("Restricted")
}
}
但是,第一次当应用程序请求权限并且用户授予权限时,它会显示以下错误:
不允许照片访问,授权状态为0
并且,当用户第二次尝试将视频保存到照片库时,它会显示以下错误:
无法完成操作