Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload images with cloud kit using swift?

How do I upload and load back images from cloud kit with swift?

What attribute type do I use?

enter image description here

What code do I use? This is the code I use currently...

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

                    }
                }
        })
        }
like image 845
nachshon f Avatar asked Apr 02 '15 16:04

nachshon f


2 Answers

You need to create a CKAsset and add that to your record. You can do that with code like this:

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")
                }
            }
        })
    }
like image 55
Edwin Vermeer Avatar answered Oct 13 '22 01:10

Edwin Vermeer


Here's something similar to Edwin's answer but a little more compact. I've tested this and it works well.

This example is saving "myImage" UIImageView into "mySaveRecord" CKRecord, just replace those names with your respective 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!
    }
})
like image 25
William T. Avatar answered Oct 13 '22 01:10

William T.