Сохранить изображение в iCloud Drive непосредственно из приложения - PullRequest
1 голос
/ 06 ноября 2019

Я хочу загрузить изображение на диск iCloud из моего приложения ios. До сих пор я загружал изображения, сохраняя их локально на диске iCloud на устройстве и синхронизируя их с iCloud. Вот код, который я попробовал.

            if (!FileManager.default.fileExists(atPath: iCloudDocumentsURL.path, isDirectory: nil)) {
                do{
                    try FileManager.default.createDirectory(at: iCloudDocumentsURL, withIntermediateDirectories: true, attributes: nil)
                }catch let error {
                    print(error.localizedDescription)
                }
            }
        }

        let localDocumentsURL = FileManager.default.urls(for: FileManager.SearchPathDirectory.documentDirectory, in: .userDomainMask).last! as NSURL
        let iCloudDocumentsURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents").appendingPathComponent("MyArtworks")

         let image = UIImage(named: "testpoto2") as UIImage?
        // get the documents directory url

        // choose a name for your image
        let fileName = "image6.png"
        // create the destination file url to save your image
        let fileURL = localDocumentsURL.appendingPathComponent(fileName)
        // get your UIImage jpeg data representation and check if the destination file url already exists
        if !FileManager.default.fileExists(atPath: fileURL!.path) {
            do {
                try UIImagePNGRepresentation(image!)!.write(to: fileURL!)
                    print("Image Added Successfully")
                } catch {
                    print(error)
                }
            } else {
                print("Image Not Added")
        }

        if let iCloudDocumentsURL = iCloudDocumentsURL {
            var isDir:ObjCBool = false
            if (FileManager.default.fileExists(atPath: iCloudDocumentsURL.path, isDirectory: &isDir)) {
                do{
                    try FileManager.default.removeItem(at: iCloudDocumentsURL)
                }catch let error {
                        print(error.localizedDescription)
                }
            }

            do{
                try FileManager.default.copyItem(at: localDocumentsURL as URL   , to: iCloudDocumentsURL)
            }catch let error {
                    print(error.localizedDescription)
            }
        } 

Но мне нужно сохранить изображения непосредственно в iCloud. Любые предложения приветствуются.

...