How to upload images using a cloud set using swift?

How to upload and download reverse images from a cloud set with fast?

What type of attribute am I using?

enter image description here

What code am i using? This is the code I'm currently using ...

func SaveImageInCloud(ImageToSave: UIImage) { let newRecord:CKRecord = CKRecord(recordType: "ImageRecord") newRecord.setValue(ImageToSave, forKey: "Image") if let database = self.privateDatabase { database.saveRecord(newRecord, completionHandler: { (record:CKRecord!, error:NSError! ) in if error != nil { NSLog(error.localizedDescription) } else { dispatch_async(dispatch_get_main_queue()) { println("finished") } } }) } 
+6
source share
4 answers

You need to create a CKAsset and add it to your entry. You can do this with code as follows:

 func SaveImageInCloud(ImageToSave: UIImage) { let newRecord:CKRecord = CKRecord(recordType: "ImageRecord") let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask if let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) { if paths.count > 0 { if let dirPath = paths[0] as? String { let writePath = dirPath.stringByAppendingPathComponent("Image2.png") UIImagePNGRepresentation(ImageToSave).writeToFile(writePath, atomically: true) var File : CKAsset? = CKAsset(fileURL: NSURL(fileURLWithPath: writePath)) newRecord.setValue(File, forKey: "Image") } } } if let database = self.privateDatabase { database.saveRecord(newRecord, completionHandler: { (record:CKRecord!, error:NSError! ) in if error != nil { NSLog(error.localizedDescription) } else { dispatch_async(dispatch_get_main_queue()) { println("finished") } } }) } 
+3
source

Here is something similar to Edwin, but a little more compact. I tested this and it works well.

In this example, "myImage" UIImageView is stored in CKRecord "mySaveRecord", just replace these names with the appropriate ones.

 let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String let imageFilePath = documentDirectory.stringByAppendingPathComponent("lastimage") UIImagePNGRepresentation(myImage).writeToFile(imageFilePath, atomically: true) let asset = CKAsset(fileURL: NSURL(fileURLWithPath: imageFilePath)) mySaveRecord.setObject(asset, forKey: "ProfilePicture") CKContainer.defaultContainer().publicCloudDatabase.saveRecord(mySaveRecord, completionHandler: { record, error in if error != nil { println("\(error)") } else { //record saved successfully! } }) 
+1
source

This answer works with Swift 2.2 and iOS 9 and separates file creation from downloading, so you can test them correctly, as they represent different actions with their own potential problems.

For uploadPhoto, the recordType variable is the value that you use in the CloudKit dashboard. The "photo" key on the line photo["photo"] = asset is the name of the field for your post type.

 func uploadPhoto(image: UIImage, recordName: String) { let privateDB = CKContainer.defaultContainer().privateCloudDatabase let photoID = CKRecordID(recordName: recordName) let photo = CKRecord(recordType: recordType, recordID: photoID) let asset = CKAsset(fileURL: writeImage(image)) photo["photo"] = asset privateDB.saveRecord(photo) { (record, error) in guard error == nil else { print(error?.localizedDescription) return } print("Successful") } } func writeImage(image: UIImage) -> NSURL { let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! let fileURL = documentsURL.URLByAppendingPathComponent(NSUUID().UUIDString + ".png") if let imageData = UIImagePNGRepresentation(image) { imageData.writeToURL(fileURL, atomically: false) } return fileURL } 

You can call this with the following:

 uploadPhoto(UIImage(named: "foo.png")!, recordName: "bar") 
0
source

You need to select the type of Asset value in the toolbar for that value.

newRecord.setValue(ImageToSave, forKey: "Image")

UIImage is not a valid type on CKRecord . Your best option is to write this image to a file and then create a CKAsset and set it to a record.

-1
source

Source: https://habr.com/ru/post/984628/


All Articles