I'm trying to send a user image from my iOS app to a Python script through Firebase by creating a base64 string from the image and then posting that string to Firebase and decoding it in Python. However, a corrupted image is produced. How do I fix this? Here is my Swift code:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
var byteArray = NSData()
if let file = info[UIImagePickerControllerOriginalImage] as? UIImage {
byteArray = UIImageJPEGRepresentation(file, 1.0)!
}
let b64 = byteArray.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
FIRDatabase.database().reference().child("dataUploaded").setValue(b64)
uploaded = true
dismissViewControllerAnimated(true, completion: nil)
}
And then the Python code:
from firebase import firebase
import os
from PIL import Image
import numpy as np
import io
fb = firebase.FirebaseApplication("https://xxxxxx.firebaseio.com/", None)
a = fb.get('/dataUploaded', None)
filename = 'image.png'
with open(filename, 'wb') as f:
f.write(a)