Skip to content

Instantly share code, notes, and snippets.

@rdelrosario
Last active February 19, 2018 08:35
Show Gist options
  • Save rdelrosario/8d7dd79550489a6de886484969da7b6d to your computer and use it in GitHub Desktop.
Save rdelrosario/8d7dd79550489a6de886484969da7b6d to your computer and use it in GitHub Desktop.
//Llamo ese codigo para coger la imagen del picker
let image = imageView.image
if image != nil{
let imageData:Data = UIImageJPEGRepresentation(image!, 1.0)!
let path:String = self.documentsDirectoryPath.appendingPathComponent(self.tempImageName)
try? UIImageJPEGRepresentation(image!, 1.0)!.write(to: URL(fileURLWithPath: path), options: [.atomic])
self.imageURL = URL(fileURLWithPath: path)
try? imageData.write(to: self.imageURL, options: [.atomic])
let File:CKAsset? = CKAsset(fileURL: URL(fileURLWithPath: path))
CloudKitHelper.setMyFullName(loginTextField.text!, image: File)
//Despues se la paso a la funcion de setFullName
static func setMyFullName(_ fullName: String , image: CKAsset?) {
CKContainer.default().fetchUserRecordID { (recordID, error) in
guard error == nil else {
debugPrint("Error \(error!.localizedDescription)")
return
}
if let userID = recordID {
debugPrint("I am \( userID.recordName )")
publicDatabase.fetch(withRecordID: userID, completionHandler: { (record, error) in
guard error == nil,
record != nil else {
debugPrint("Cannot get my own user record")
return
}
record!["fullName"] = fullName as NSString
publicDatabase.save(record!, completionHandler: { (record, error) in
if error != nil {
debugPrint("Message creation error: \( error!.localizedDescription )")
return
}
debugPrint("User saved with id \( record?.recordID.recordName ?? "no-name" )")
})
//Save Image
if image != nil {
//let newRecord:CKRecord = CKRecord(recordType: "Image")
record!.setObject(image, forKey: "image")
publicDatabase.save(record!, completionHandler: { (record:CKRecord?, error:Error?) in
// Check if there was an error
if error != nil {
NSLog((error?.localizedDescription)!)
}
else if let record = record {
// Do whatever you want with the record, but image record was saved, asset should be saved.
}
})
}
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment