ios - How to receive an image from cloudkit? -
i using code store image in icloud, code use retrieave , place in uiimageview? i've tried everything, 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") } } }) }
just read ckrecord wrote , can ckasset reading key image. can uiimage using 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 }