简体   繁体   English

将图像上传到Firebase

[英]Uploading images to Firebase

I'm trying to upload images to Firebase like this: 我正在尝试将图像上传到Firebase,如下所示:

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: 但是这不起作用,因为字符串超过了最大大小:

Terminating app due to uncaught exception 'InvalidFirebaseData', reason: '(updateChildValues:) String exceeds max size of 10485760 utf8 bytes:

What can I do to resolve this problem? 我该怎么做才能解决这个问题? I haven't found anything online in regards to iOS development and images when using Firebase. 在使用Firebase时,我没有在iOS开发和图像方面找到任何在线内容。

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? 我自己引用一下: 如何通过android将文件保存到Firebase Hosting文件夹位置以获取图像?

The Firebase Database allows you to store JSON data. Firebase数据库允许您存储JSON数据。 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. 虽然二进制数据不是JSON支持的类型,但可以将二进制数据编码为base64,从而将图像存储在(大)字符串中。 [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. 您最好的选择通常是将图像存储在第三方图像存储服务上。

As Frank van Puffelen suggested, my solution was to use Amazon S3 for imagine storage, and use Firebase to store a reference to the image location. 正如Frank van Puffelen建议的那样,我的解决方案是使用Amazon S3进行想象存储,并使用Firebase存储对图像位置的引用。

I created a method called uploadImage: and it looks like this: 我创建了一个名为uploadImage:的方法uploadImage:它看起来像这样:

-(void)uploadImage:(UIImage *)image
{
// Create reference to Firebase
Firebase *ref = [[Firebase alloc] initWithUrl:@"https://<MY-APP>.firebaseio.com"];
Firebase *photosRef = [ref childByAppendingPath:@“photos];
Firebase *newPhotoRef = [photosRef childByAutoId];

// Image information
NSString imageId = [[NSUUID UUID] UUIDString];

// Create dictionary containing information
NSDictionary photoInformation = @{
                                @“photo_id” : imageId
                                // Here you can add more information about the photo
                                };

NSString *imagePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageId]];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:imagePath atomically:YES];

NSURL *imageUrl = [[NSURL alloc] initFileURLWithPath:imagePath];
AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
uploadRequest.bucket = @“<AMAZON S3 STORAGE NAME>“; // create your own by setting up an account on Amazon aws.
uploadRequest.key = imageId;
uploadRequest.contentType = @"image/png";
uploadRequest.body = imageUrl;

AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
[[transferManager upload:uploadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor] withBlock:^id(AWSTask *task) {
    if (!task.error) {
        // Update Firebase with reference
        [newPhotoRef updateChildValues:currentPHD withCompletionBlock:^(NSError *error, Firebase *ref) {
            if (!error) {
                [newPhotoRef updateChildValues:photoInformation withCompletionBlock:^(NSError *error, Firebase *ref) {
                    if (!error) {
                        // Uploaded image to Amazon S3 and reference to Firebase
                    }
                }];
            }
        }];
    } else {
        // Error uploading
}
return nil;
}];
}

Edit The method should be a block method, something like this: 编辑该方法应该是一个块方法,如下所示:

-(void)uploadImage:(UIImage *)image withBlock:(void (^)(Firebase *ref, NSError *error, AWSTask *task))handler
{
    // upload
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM