Python 解码base64字符串时图像损坏

Python 解码base64字符串时图像损坏,python,ios,swift,firebase,firebase-realtime-database,Python,Ios,Swift,Firebase,Firebase Realtime Database,我试图通过Firebase将iOS应用程序中的用户图像发送到Python脚本,方法是从图像中创建一个base64字符串,然后将该字符串发送到Firebase并用Python对其进行解码。但是,会生成损坏的图像。我该如何解决这个问题?这是我的Swift代码: func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])

我试图通过Firebase将iOS应用程序中的用户图像发送到Python脚本,方法是从图像中创建一个base64字符串,然后将该字符串发送到Firebase并用Python对其进行解码。但是,会生成损坏的图像。我该如何解决这个问题?这是我的Swift代码:

    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)
    }
然后是Python代码:

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)

我总是对base64中的png文件(或者可能是gif)有问题。。。但这可能是因为我做错了什么。。。我建议您尝试使用jpg(我想您会发现它很管用,尽管这并不能真正解决您的问题,而且对您来说可能不是一个可行的解决方案)Firebase不赞成使用这种存储图像的方法,而支持使用新的,更好的一个:我的问题是Firebase存储不适合Python,因为无法获得图像的实际下载url(下载url与Firebase存储上的文件路径不同)。但是base64编码不再有效了吗?@JoranBeasley我试着使用jpeg格式,但也不起作用。我一直对base64中的png文件(或者可能是gif)有问题。。。但这可能是因为我做错了什么。。。我建议您尝试使用jpg(我想您会发现它很管用,尽管这并不能真正解决您的问题,而且对您来说可能不是一个可行的解决方案)Firebase不赞成使用这种存储图像的方法,而支持使用新的,更好的一个:我的问题是Firebase存储不适合Python,因为无法获得图像的实际下载url(下载url与Firebase存储上的文件路径不同)。但是base64编码不再有效了吗?@JoranBeasley我尝试使用jpeg格式,但也不起作用。