1

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)
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Abhi V
  • 714
  • 1
  • 4
  • 19
  • ive always had issues with png files in base64(or maybe it was gif) ... but its probably cause im doing something wrong ... I would recommend trying with jpg (I think youll find it works , even though this doesnt really solve your issue, and may not be a viable solution for you) – Joran Beasley Jul 21 '16 at 18:35
  • Firebase deprecated this approach to storing images in in favor of a new, better one: http://stackoverflow.com/a/13957446/788532 – danielmhanover Jul 21 '16 at 19:30
  • my problem is that Firebase Storage is not good with Python because theres no way to get an image's actual download url (the download url is different from the file's path on Firebase Storage). But does base64 encoding not work anymore? – Abhi V Jul 21 '16 at 20:43
  • @JoranBeasley I tried to use jpeg format but it also didn't work. – Abhi V Jul 21 '16 at 20:46

0 Answers0