Skip to content

Instantly share code, notes, and snippets.

@makthrow
Last active November 28, 2016 05:33
Show Gist options
  • Save makthrow/d68b23abd3389b69f782 to your computer and use it in GitHub Desktop.
Save makthrow/d68b23abd3389b69f782 to your computer and use it in GitHub Desktop.
saving and getting images in base64 string to Firebase
var data: NSData = NSData()
if let image = photoImageView.image {
data = UIImageJPEGRepresentation(image,0.1)!
}
let base64String = data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
let user: NSDictionary = ["name":name!,"dob":dateOfBirthTimeInterval, "photoBase64":base64String]
//add firebase child node
let profile = firebase.ref.childByAppendingPath(name!)
// Write data to Firebase
profile.setValue(user)
ref.observeEventType(.Value, withBlock: { snapshot in
self.base64String = snapshot.value as String
let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
if let decodedImage = UIImage(data: decodedData!) {
imageView.image = decodedImage
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment