The operation couldn’t be completed. (Cocoa error -1.) - PHPhotoLibrary

穿精又带淫゛_ 提交于 2019-12-10 17:16:19

问题


Hi I am trying to save a downloaded mp4 file to my gallery.

Downloading the url by using , downloadTask

in

func urlSession(_ session: URLSession,
                    downloadTask: URLSessionDownloadTask,
                    didFinishDownloadingTo location: URL){
}

I am getting the url

file:///private/var/mobile/Containers/Data/Application/8D761DFE-C050-426D-B91C-61DE441FB496/Library/Caches/com.apple.nsurlsessiond/Downloads/com.te.dn/CFNetworkDownload_KFSYSW.tmp

I am creating a path by using,

let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
        let documentDirectoryPath:String = path[0]
        let fileManager = FileManager()
        let destinationURLForFile = URL(fileURLWithPath: documentDirectoryPath.appendingFormat("/file1.mp4"))

if fileManager.fileExists(atPath: destinationURLForFile.path){

        }
        else{
            do {
                try fileManager.moveItem(at: location, to: destinationURLForFile)
                // show file

            }catch{
                print("An error occurred while moving file to destination url")
            }
        }

I could successfully move the file (not getting any error while moving).

Then I trying to save the file getting error

let url = URL(fileURLWithPath: destinationURLForFile.path)

url value is :

file:///var/mobile/Containers/Data/Application/8DF0C849-56A1-42B3-A081-0C992D18F973/Documents/file1.mp4


        PHPhotoLibrary.shared().performChanges({
            PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: url)
        }) { saved, error in
            if saved {
                let alertController = UIAlertController(title: "Your video was successfully saved", message: nil, preferredStyle: .alert)
                let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                alertController.addAction(defaultAction)
                self.present(alertController, animated: true, completion: nil)
            }
            else
            {
                print(error?.localizedDescription ?? "f")
            }

        }

o/p : Error :- The operation couldn’t be completed. (Cocoa error -1.)


回答1:


Can not save video from m3u8 file.

But, we can offline-download the video, Also can't move to video to gallery.

Here the apple code HLSCatalog



来源:https://stackoverflow.com/questions/47068666/the-operation-couldn-t-be-completed-cocoa-error-1-phphotolibrary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!