Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to receive an image from cloudkit?

I am using this code to store an image in icloud, but what code do i use to retrieave it and place it in a UIImageView? I've tried everything, but it wont work?

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 699
nachshon f Avatar asked Apr 09 '15 20:04

nachshon f


People also ask

What is CK asset files?

An external file that belongs to a record.

How do I add a record to CloudKit?

From the CloudKit Desktop, select Schema > Record Types > New Type, and give it the name of Task. Adding the Task Registry Type. Once this type of record is created, we select it and start adding fields (after adding them, click on Save): title (of type String)


2 Answers

Just read the CKRecord that you wrote and you can get the CKAsset by reading the key Image. You can get a UIImage using the code below.

var file : CKAsset? = record.objectForKey("Image")

func image() -> UIImage? {
    if let file = file {
        if let data = NSData(contentsOfURL: file.fileURL) {
            return UIImage(data: data)
        }
    }
    return nil
}
like image 88
Edwin Vermeer Avatar answered Sep 26 '22 02:09

Edwin Vermeer


You have to first have a way of finding the specific ImageRecord that you want to retrieve. Assuming that you have the RecordID for the ImageRecord you saved (you can get this from the record in the saveRecord completion block) you can do:

if let database = privateDatabase {
    database.fetchRecordWithID(recordID, completionHandler: { (record, error) -> Void in
        guard let record = record else {
            print("Error retrieving record", error)
            return
        }

        guard let asset = record["Image"] as? CKAsset else {
            print("Image missing from record")
            return
        }

        guard let imageData = NSData(contentsOfURL: asset.fileURL) else {
            print("Invalid Image")
            return
        }

        let image = UIImage(data: imageData)
        imageView.image = image
    })
}

(Although you would definitely want to be doing some error handling where those print()s are)

If you don't save the recordID (or probably better: the recordID.recordName so you can make another CKRecordID later), you would need some other way of finding which record you are looking for. If that's the case you'd want to look into using CKDatabase's performQuery(_:inZoneWithID:completionHandler:) method.

like image 36
Marcus Avatar answered Sep 27 '22 02:09

Marcus