该操作无法完成. (可可错误-1.)-PHPhotoLibrary [英] The operation couldn’t be completed. (Cocoa error -1.) - PHPhotoLibrary

查看:606
本文介绍了该操作无法完成. (可可错误-1.)-PHPhotoLibrary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将下载的mp4文件保存到我的画廊.

使用,downloadTask

下载网址

func urlSession(_ session: URLSession,
                    downloadTask: URLSessionDownloadTask,
                    didFinishDownloadingTo location: 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

我正在使用创建路径,

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")
            }
        }

我可以成功移动文件(移动时不会出现任何错误).

然后我尝试保存文件时出错

let url = URL(fileURLWithPath: destinationURLForFile.path)

网址值为:

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:错误:-The operation couldn’t be completed. (Cocoa error -1.)

解决方案

无法保存m3u8文件中的视频.

但是,我们可以离线下载视频,也不能将视频移至图库.

这里是苹果代码 HLSCatalog

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.)

解决方案

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

这篇关于该操作无法完成. (可可错误-1.)-PHPhotoLibrary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆