我尝试通过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)