将图像上传到Firebase [英] Uploading images to Firebase
问题描述
我想像这样上传图片到Firebase:
I'm trying to upload images to Firebase like this:
Firebase *ref = [[Firebase alloc] initWithUrl:@"https://<app-name>.firebaseio.com/posts"];
Firebase *newPost = [ref childByAutoId];
NSDictionary *newPostData = @{
@"image" : [self encodeToBase64String:image]
};
[newPost updateChildValues:newPostData];
我使用此代码对图像进行编码:
I'm using this code to encode the image:
- (NSString *)encodeToBase64String:(UIImage *)image {
return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
但这不工作,因为字符串超出最大大小:
But this does not work as the string exceeds the maximum size:
由于未捕获异常而终止应用程序InvalidFirebaseData,reason:'(updateChildValues :)字符串超过最大大小10485760 utf8字节:
我该如何解决这个问题?
What can I do to resolve this problem? I haven't found anything online in regards to iOS development and images when using Firebase.
推荐答案
如果图片太大,你应该存储一个较小的图像。让我引用自己:如何通过Android将文件保存到Firebase主机文件夹中的映像位置?
If the image is too big, you should store a smaller image. Let me quote myself: How do you save a file to a Firebase Hosting folder location for images through android?
Firebase数据库允许您存储JSON数据。虽然二进制数据不是JSON支持的类型,但可以对base64中的二进制数据进行编码,从而将图像存储在(大)字符串中。 [但是]虽然这是可能的,但它不建议任何东西,但小图像或作为一个好奇心,看看它可以做。
The Firebase Database allows you to store JSON data. While binary data is not a type that is supported in JSON, it is possible to encode the binary data in say base64 and thus store the image in a (large) string. [But] while this is possible, it is not recommended for anything but small images or as a curiosity to see that it can be done.
您最好的选择通常是将图像存储在第三方图像存储服务上。
Your best option is typically to store the images on a 3rd party image storage service.
这篇关于将图像上传到Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!